Linq:嵌套子查询中的动态Where子句获取每个组的最新记录

时间:2016-05-27 04:07:10

标签: entity-framework linq linq-to-sql linq-to-entities predicatebuilder

我目前在下面的动态linq表达式中遇到问题

我的模特

public class Orders
{
    public int OrderId ;
    public ICollection<OrderStatuses> Statuses;
}  

public class Statuses
{
    public int StatusId;
    public int OrderId;
    public string Note;
    public DateTime Created;
}

我的样本数据:

Orders
|  ID   | Name       |
----------------------
|   1   | Order 01   |  
|   2   | Order 02   | 
|   3   | Order 03   | 

Statuses
|ID | OrderId | Note      | Created   |
---------------------------------------
| 1 |  1      | Ordered   | 2016-03-01|
| 2 |  1      | Pending   | 2016-04-02|
| 3 |  1      | Completed | 2016-05-19|
| 4 |  1      | Ordered   | 2015-05-19|
| 5 |  2      | Ordered   | 2016-05-20|
| 6 |  2      | Completed | 2016-05-19|
| 7 |  3      | Completed | 2016-05-19|

我想获得一些票据数量等于&#39; Ordered&#39; 最大创建时间的订单数量。 以下是我期望从查询中获得的订单数量

| Name     |  Note    |  Last Created|
-------------------------------------|
| Order 01 |  Ordered | 2016-03-01   | 
| Order 02 |  Ordered | 2016-05-20   |

这是我的想法,但似乎错误的方式

var outer = PredicateBuilder.True<Order>();

var orders   = _entities.Orders
                        .GroupBy(x => x.OrderId)
                        .Select(x => new { x.Key, Created = x.Max(g => g.Created) })
                        .ToArray();

var predicateStatuses = PredicateBuilder.False<Status>();

foreach (var item in orders)
{
    predicateStatuses = predicateStatuses.Or(x => x.OrderId == item.Key && x.Created == item.Created);
}
var predicateOrders = PredicateBuilder.False<JobOrder>();

predicateOrders = predicateOrders.Or(predicateStatuses); (I don't how to passed expression which different object type (Order and Status) here or I have to write an extension method or something

outer = outer.And(predicateOrders);

在这种情况下,请建议我如何解决这个动态linq表达式。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的查询没有任何动态,至少,它不是必须的。您可以将其表达为常规查询。

var query =
    from o in db.Orders
    join s in db.Statuses on o.Id equals s.OrderId
    where s.Note == "Ordered"
    orderby s.Created descending
    group new { o.Name, s.Note, LastCreated = s.Created } by o.Id into g
    select g.First();

p.s。,你的模型似乎根本不匹配数据,所以我忽略了这一点。根据需要进行调整。

答案 1 :(得分:0)

非常感谢@Jeff Mercado的回答。最后,我自定义了你的答案来解决我的问题:

var predicateStatuses = PredicateBuilder.False<Order>();
predicateStatuses = predicateStatuses.Or(p => (
                                        from j in db.Statuses 
                                        where j.OrderId == p.ID
                                        group j by j.OrderId into g
                                        select g.OrderByDescending(t=>t.Created)
                                        .FirstOrDefault()
                                    ).FirstOrDefault().Note == 'Ordered'
                                );