简体   繁体   English

从实体列表中查找实体

[英]Find entities from list of entities

I have a list of entities like below. 我有如下实体列表。

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}

I want to find the entities which have the oldest from date and latest last date. 我想找到最旧的起始日期和最新的终止日期的实体。 ( entities covering whole from date and to date) (涵盖日期至今的整个实体)

Basically I need to get the entities which have distinct from and to dates. 基本上,我需要获取与日期不同的实体。 Also the starting entity should have the oldest From date and final entity should have the latest To date 此外,起始实体应具有最旧的起始日期,而最终实体应具有最新的截止日期

In this case the expected result is entities starting from 01/01/2011 to 10/04/2011 . 在这种情况下,预期结果是从01/01/201110/04/2011的实体。

Update 更新资料

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

I tried something like this 我尝试过这样的事情

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();  

But I think it should do without grouping, but I am not quite sure 但是我认为应该不进行分组,但是我不确定

Any help is appreciate 任何帮助表示赞赏

Thanks 谢谢

var dates = from e in EntityList
            orderby e.Id
            group e by new { e.From, e.To } into g
            select g.First();

Query will return only first (ordered by id) entities from groups with same from-to dates. 查询将仅返回起始日期相同的组中的第一个(按ID排序)实体。

Couldn't you use something like this: 你不能使用这样的东西:

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

UPDATE: 更新:

Could you use the Distinct method? 您可以使用Distinct方法吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM