Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. LINQ. Mapping;
Using system. Data. LINQ;
Namespace SCSI
{
[Table (name = "MERs")] // map the customers table in the database to the customer class.
Public Class Customer
{
[Column] // column as the name suggests
Public String customerid {Get; set ;}
[Column]
Public String city {Get; set ;}
Public override string tostring ()
{
Return customerid + "\ t" + city;
}
}
Class Program
{
Static void main (string [] ARGs)
{
// Numquery ();
Objectquery ();
Console. Read ();
}
Static ienumerable <customer> createcustomers ()
{
Return new list <customer>
{
New customer {customerid = "alfki", city = "Berlin "},
New customer {customerid = "bonap", city = "Marseille "},
New customer {customerid = "consh", city = "London "},
New customer {customerid = "eastc", city = "London "},
New customer {customerid = "Frans", city = "Torino "},
New customer {customerid = "lonep", city = "Portland "},
New customer {customerid = "norts", city = "London "},
New customer {customerid = "thebi", city = "Portland "}
};
}
Static void objectquery ()
{
// Datacontext is the most basic and important thing of LINQ. It is the entry point of the LINQ to SQL statement. For details, please query the msdn
VaR DB = new datacontext (@ "Data Source = shni010 \ sqlexpress; initial catalog = northwind; Integrated Security = true ");
// Var Results = from C in createcustomers ()
// Where C. City = "London"
// Select C. customerid;
// The query statement of LINQ
VaR Results = from C in db. gettable <customer> ()
Where C. City = "London"
Select C;
DB. log = console. out;
Foreach (var c in results) // loop result set
{
Console. writeline ("{0} \ t {1}", C. customerid, C. City );
}
Console. Read ();
}
// The following method shows the query of arrays by LINQ.
Static void numquery ()
{
VAR numbers = new int };
VaR evennumbers = from P in numbers
Where (P % 2) = 0
Select P;
Console. writeline ("Result :");
Foreach (var val in evennumbers)
{
Console. writeline (VAL );
}
Console. Read ();
}
}
}