Linq To Entities 的语法基础可以参考:
Microsoft Language Integrated Query (LINQ) 从入门到精通
1.使用ObjectQuery类的ToTraceString()方法显示查询SQL语句
Console.WriteLine(customers.ToTraceString());
2.基于表达式的查询语法
ObjectQuery<Customers> customers = edm.Customers;
IQueryable<Customers> cust1 = from c in customers
where c.CustomerID == "ALFKI"
select c;
3.基于方法的查询语法
var cust2 = edm.Customers.Where(c => c.CustomerID == "ALFKI");
4.排序分页
IQueryable<Customers> cust10 = (from c in customers
orderby c.CustomerID
select c).Skip(0).Take(10);
或者
var cust = edm.Customers.OrderBy(c => c.CustomerID).Skip(0).Take(10);
5.可使用的聚合运算符有Average、Count、Max、Min 和 Sum。
var maxuprice = edm.Products.Max(p => p.UnitPrice);
Console.WriteLine(maxuprice.Value);
6. 可以的连接有Join 和 GroupJoin 方法。GroupJoin组联接等效于左外部联接,它返回第一个(左侧)数据源的每个元素(即使其他数据源中没有关联元素)。
using (var edm = new NorthwindEntities())
{
var query = from d in edm.Order_Details
join order in edm.Orders
on d.OrderID equals order.OrderID
select new
{
OrderId = order.OrderID,
ProductId = d.ProductID,
UnitPrice = d.UnitPrice
};
foreach (var q in query)
Console.WriteLine("{0},{1},{2}",q.OrderId,q.ProductId,q.UnitPrice);
}
参考文章:
http://www.pin5i.com/showtopic-24045.html
未完成!