0

我有一个如下实体列表。

Entity1 { Id=1, From=01/01/2011, To=31/01/2011, Status=true }
Entity2 { Id=1, From=01/02/2011, To=28/02/2011, Status=false}
Entity2 { Id=2, From=01/02/2011, To=28/02/2011, Status=false}
Entity3 { Id=3, From=01/01/2011, To=31/01/2011, Status=true }
Entity4 { Id=4, From=01/01/2011, To=31/01/2011, Status=true }
Entity5 { Id=1, From=01/03/2011, To=31/03/2011, Status=false}
Entity6 { Id=4, From=31/03/2011, To=10/04/2011, Status=false}

我想找到具有最旧日期和最晚日期的实体。(实体涵盖从日期到至今的全部内容)

基本上我需要获取与日期不同的实体。此外,起始实体应具有最早的起始日期,最终实体应具有最新的截止日期

在这种情况下,预期结果是从01/01/201110/04/2011的实体。

更新

Entity 1 has the oldest from date : 01/01/2011
Entiry 2 which falls under : 01/01/2011 and 10/04/2011
Entity 3 which falls under 01/01/2011 and 10/04/2011(but entity 1 has same from and to date as Entity3 so entity 3 not a valid)
Entity 4 which falls under 01/01/2011 and 10/04/2011(but entity 1 has same from and to date as Entity4 so entity 4 not a valid)
Entity 5 which falls under : 01/01/2011 and 10/04/2011
Entity 6 has the most recent To date : 10/04/2011
So Valid entries are Entity1, Entity2, Entity5, Entity6

我尝试过这样的事情

var dates = (EntityList.GroupBy(offer => offer.id)
             .Select(group =>
                 new { offerid = group.Key,
                       offers = group.OrderBy(o => o.From)
                       })
             .OrderBy(g => g.offers.First().From) 
        . FirstOrDefault
 ().offers.First();  

但我认为它应该不分组,但我不太确定

任何帮助表示赞赏

谢谢

4

2 回答 2

0

你不能使用这样的东西:

var selectedEntity = EntityList.OrderBy(e => e.From)
                               .ThenByDescending(e => e.To)
                               .FirstOrDefault();

更新:

你可以使用 Distinct 方法吗?

var selectedEntity = EntityList.OrderBy(e => e.From)
                               .ThenByDescending(e => e.To)
                               .Distinct();
于 2012-10-23T16:01:34.107 回答
0
var dates = from e in EntityList
            orderby e.Id
            group e by new { e.From, e.To } into g
            select g.First();

查询将仅返回具有相同起始日期的组中的第一个(按 id 排序)实体。

于 2012-10-23T15:46:05.650 回答