发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
假如想查询拖欠按揭超过30天的银行账号,同时查询出他们的单据,并且需要按照单据日期进行排序,这样可以首先看到最近的单据,方便找出问题。
大多数人都知道EF可以使用Include()热加载关系实体,例如这样:
var lateCustomers = from c in ctx.Customers.Include("Orders") where c.IsMoreThan30DaysInArrears select c;
但是很不幸,这样找到的每个客户的单据顺序都是随机的。
那么我们怎么来对他们进行排序呢,严格讲那是没有答案的。
但是我们可以稍微做些改变,如干掉Include,使用查询语句。
var lateCustomers = from c in ctx.Customers where c.IsMoreThan30DaysInArrears select new { Customer = c, Orders = c.Orders.OrderByDescending( o => o.OrderDate ) };
这样做使用了标准的Linq语义来枚举查询匿名类型,这样做达到了两个效果:顾客和他的有序单据。
EF对此支持的很好,这样问题就解决了。
来自: 昵称10504424 > 《工作》
0条评论
发表
请遵守用户 评论公约
sql exist-sql语句(十三) -bahe-sql's blog
SELECT *FROM suppliersWHERE EXISTS(select *from orderswhere suppliers.supplier_id = orders.supplier_id);SELECT *FROM suppliersWHERE not exists (select * from orders Where suppliers.suppli...
linq to sql简单使用
Contains("a") //等价于 like ''%a%'' //where employee.FirstName.StartsWith("a") //等价于 lik...
自查询替换为连接查询的方法
CREATE TABLE ranks( title varchar(30), rank varchar(30) );select payment from salary where rank=( SELECT rank from ranks where title=( SELECT title from jobs where ...
XmlDocument,XDocument相互转换
XmlDocument,XDocument相互转换。XElement element = new XElement(ns + "elementName");LINQ to XML also works really well with LINQ - its construction model allows you to build ele...
SQL表关联查询 (各种 join)
SQL表关联查询 (各种 join)select a.id,a.va,b.id,b.vb from @ta a left join @tb bon a.id=b.idselect a.id,a.va,b.id,b.vb from @ta a left outer join @tb bon a.id=b.id.A:select a.*,b.* from t...
SQL连接查询深度探险
下面举例说明: 语句5:左外连接(LEFT OUTER JOIN)SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAMEFROM ORDERS O LEFT OUTER J...
SQL各种连接查询详解
from orders o left outer join customers c on c.id=o.customer_id.from orders o left outer join customers c on c.id=o.customer_id and o.order_number<>''mike_order001''...
你真的会玩SQL吗?EXISTS和IN之间的区别
where exists (select t1.orderid from Test1 as t1 where t1.[name]=t.[name])1.先处理外层查询,从Sales.Customers表别名C中取出一个...
使用Gorm进行高级查询
使用Gorm进行高级查询。Find(&filteredProducts)GORM中的连接和关联。Find(&usersWithOrders)在GORM中预加载相关数据。使用GORM...
微信扫码,在手机上查看选中内容