简体   繁体   English

通过使用linq比较两个列表进行排序?

[英]sorting by using linq comparing two lists?

I have 2 list of items; 我有2个项目清单;

IEnumerable<Investigator> investigators = RepositoryContext.InvestigatorRep.GetInvestigators(site.Site.Id, out totalCount);

var NewInvestigators = base.ActivePage.Investigators;

I have 30 items in investigators and 20 items in NewInvesigators, both have property Id, InvId I need to match that. 我在调查员中有30个项目,在NewInvesigators中有20个项目,都具有属性ID,InvId,我需要对此进行匹配。

     var all = investigators.Where(b => crInvestigators.Any(a => a.InvestigatorId == b.Id));

I tried this but not worked I want to create a new list based on matching Id of those two lists. 我尝试了此操作,但是没有用,我想根据这两个列表的匹配ID创建一个新列表。 If Id matches get the particular investigator(basically a sort on investigators based on Id existing in NewInvesigators). 如果Id匹配,则获取特定的调查员(基本上是根据NewInvesigators中现有的ID对调查员进行排序)。

I can do it by using for each, but I want to know whether it is possible with linq ? 我可以通过为每个使用来做到这一点,但是我想知道linq是否可行?

in newinvestigator I have object which is having two property, investigatorId and Name. 在newinvestigator中,我有一个对象,它具有两个属性,investigatorId和Name。

In Investigator I have property Id , City , country. 在调查器中,我具有属性ID,城市,国家。 no Name or investigatorId in the investigator list. 调查者列表中没有Name或researchigatorId。

You could try something like this: 您可以尝试这样的事情:

var result = investigators.Where(inv=>NewInvestigators.Any(ninv=>ninv.id == inv.investigatorId))
                          .OrderBy(inv=>inv.id);

Another way to get the same result is using a join . 获得相同结果的另一种方法是使用join

var result = from inv in investigators
             join ninv in NewInvestigators
             on inv.id equals ninv.investigatorId 
             order by inv.id
             select inv;

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

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