Linq To Sql - Example 1 : Where -1

1:44 AM
Linq Query:

//Where -1
            NorthWindDataContext NwDC = new NorthWindDataContext();

            var cust =
                from c in NwDC.Customers
                where c.City.StartsWith("L")
                select c;
            foreach (var t in cust)
            {
                Console.WriteLine("Name: {0} , City : {1} ", t.ContactName, t.City);
            }

//


Output:

Name: Thomas Hardy , City : London
Name: Christina Berglund , City : Luleå
Name: Victoria Ashworth , City : London
Name: Elizabeth Brown , City : London
Name: Ann Devon , City : London
Name: Martine Rancé , City : Lille
Name: Lino Rodriguez , City : Lisboa
Name: Alexander Feuer , City : Leipzig
Name: Simon Crowther , City : London
Name: Isabel de Castro , City : Lisboa
Name: Hari Kumar , City : London
Name: Art Braunschweiger , City : Lander
Name: Mary Saveley , City : Lyon

Sql Equivalent Query:

SELECT  ContactName
      , City
FROM    dbo.Customers
WHERE   city LIKE 'L%'


Output:

Thomas Hardy      London
Christina Berglund    Luleå
Victoria Ashworth    London
Elizabeth Brown     London
Ann Devon     London
Martine Rancé     Lille
Lino Rodriguez     Lisboa
Alexander Feuer     Leipzig
Simon Crowther     London
Isabel de Castro     Lisboa
Hari Kumar        London
Art Braunschweiger       Lander
Mary Saveley        Lyon


Explanation: We are simply listing all the Customer who are living in the city name Staring With 'L' letter.

For How to Setup Database and Linq to Sql Class Please follow the instructions here: Setting Up Linq to Sql Class

0 comments:

Post a Comment