LINQ to Entities 常用查询语句

前端之家收集整理的这篇文章主要介绍了LINQ to Entities 常用查询语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

要使用关联实体返回局部或完整的对象图,需要在LINQ 标准查询操作符之前执行Include()操作符,如下列查询所示,它将返回和前面“使用Include()操作符进行预先加载”一节同样的结果:

 
 
  1. C#3.0
  2. using(NorthwindEntitiesocNwind=new
  3. NorthwindEntities("name=NorthwindEntities"))
  4. {
  5. List<Order>orderList=null;
  6. ObjectQuery<Order>orders=ocNwind.Orders;
  7. orders.MergeOption=MergeOptions.AppendOnly;
  8. varorderQuery=orders.Include("Order_Details")
  9. .Include("Customer")
  10. .Include("Employee")
  11. .Include("Shipper")
  12. .Where(o=>o.Customer.Country=="Brazil")
  13. .OrderByDescending(o=>o.OrderID)
  14. .Select(o=>o)
  15. .Take(5);
  16. foreach(OrderorderinorderQuery)
  17. {
  18. //Materializetheobject
  19. orderList.Add(order);
  20. }
  21. }
  22. VB9.0
  23. UsingocNwindAsNewNorthwindEntities("name=NorthwindEntities")
  24. DimorderListAsList(OfOrder)=Nothing
  25. DimoqOrdersAsObjectQuery(OfOrder)=ocNwind.Orders
  26. orders.MergeOption=MergeOptions.AppendOnly
  27. DimorderQuery=oqOrders.Include("Order_Details")_
  28. .Include("Customer")_
  29. .Include("Employee")_
  30. .Include("Shipper")_
  31. .Where(Function(o)o.Customer.Country="Brazil")_
  32. .OrderByDescending(Function(o)o.OrderID)_
  33. .Select(Function(o)o)_
  34. .Take(5)
  35. ForEachoqOrderAsOrderInorderQuery
  36. 'Materializetheobject
  37. orderList.Add(oqOrder)
  38. NextoqOrder
  39. EndUsing

LINQ 表达式语法和串联方法调用区别较大。在4 个标准查询操作符执行之后,要将ObjectQuery<Order>改变为IQueryable<Order>类型,需要把Include()操作符和LINQ 表达式相分隔,如下所示:

 
 
  1. C#3.0
  2. ordersorders=orders.Include("Order_Details")
  3. .Include("Customer")
  4. .Include("Employee")
  5. .Include("Shipper");
  6. orderQuery=(fromoinorders
  7. whereo.Customer.Country=="Brazil"
  8. orderbyo.OrderIDdescending
  9. selecto).Take(5);
  10. VB9.0
  11. oqOrdersoqOrders=oqOrders.Include("Order_Details")
  12. .Include("Customer")
  13. .Include("Employee")
  14. .Include("Shipper")
  15. orderQuery=(FromoInoqOrders_
  16. Whereo.Customer.Country="Brazil"_
  17. OrderByo.OrderIDDescending_
  18. Selecto).Take(5)

可选地,可以使用下面这样更为易读的语法:

 
 
  1. C#3.0
  2. orderQuery=(fromoinorders.Include("Order_Details")
  3. .Include("Customer")
  4. .Include("Employee")
  5. .Include("Shipper")
  6. whereo.Customer.Country=="Brazil"
  7. orderbyo.OrderIDdescending
  8. selecto).Take(5);
  9. VB9.0
  10. orderQuery=(FromoInoqOrders.Include("Order_Details"_
  11. .Include("Customer")_
  12. .Include("Employee")_
  13. .Include("Shipper")_
  14. Whereo.Customer.Country="Brazil"_
  15. OrderByo.OrderIDDescending_
  16. selecto).Take(5);
原文链接:https://www.f2er.com/vb/259972.html

猜你在找的VB相关文章