假如想查询拖欠按揭超过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对此支持的很好,这样问题就解决了。

  

 

相关文章:

  • 2021-10-05
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-11-23
  • 2021-07-04
  • 2022-12-23
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案