= new Northwindcontext ()) { var= context. Products; foreach (var in expr) { Console.WriteLine (item. ProductName);} }
SELECT [Extent1].[ProductID] as [ProductID], [Extent1].[CategoryID] as [CategoryID], [Extent1].[ProductName] as [ProductName], [Extent1].[UnitPrice] as [UnitPrice], [Extent1].[discontinued] as [discontinued] from [dbo].[Product] as [Extent1]
2. Querying a specified field
var = context. Products . Select = = new {p.productid, p.productname});
var = from inch context. Products Select== P.productname};
The SQL generated by both forms is the same.
SELECT [Extent1].[ProductID] as [ProductID], [Extent1].[ProductName] as [ProductName] from [dbo].[Product] as [Extent1]
3. First/firstordefault
var = context. Products.first ();
SELECT TOP(1) [C].[ProductID] as [ProductID], [C].[CategoryID] as [CategoryID], [C].[ProductName] as [ProductName], [C].[UnitPrice] as [UnitPrice], [C].[discontinued] as [discontinued] from [dbo].[Product] as [C]
4. Single/singleordefault
var = context. Products = = =1);
SELECT TOP(2) [Extent1].[ProductID] as [ProductID], [Extent1].[CategoryID] as [CategoryID], [Extent1].[ProductName] as [ProductName], [Extent1].[UnitPrice] as [UnitPrice], [Extent1].[discontinued] as [discontinued] from [dbo].[Product] as [Extent1] WHERE 1 = [Extent1].[ProductID]
LINQ series: LINQ to SQL Select query