C# 使用linq比较两个列表进行排序?

C# 使用linq比较两个列表进行排序?,c#,linq,C#,Linq,我有两个项目清单 IEnumerable<Investigator> investigators = RepositoryContext.InvestigatorRep.GetInvestigators(site.Site.Id, out totalCount); var NewInvestigators = base.ActivePage.Investigators; 我试过了,但没有成功 我想根据这两个列表的匹配Id创建一个新列表。如果Id匹配,则获取特定的调查者(基本上是基

我有两个项目清单

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

var NewInvestigators = base.ActivePage.Investigators;
我试过了,但没有成功 我想根据这两个列表的匹配Id创建一个新列表。如果Id匹配,则获取特定的调查者(基本上是基于NewInvesigators中存在的Id对调查者进行排序)

我可以通过使用for each来实现,但我想知道是否可以使用
linq


在NewInvestor中,我有一个对象,它有两个属性,investorID和Name

在调查员中,我有财产Id、城市、国家。
调查员列表中没有姓名或调查员ID。

您可以尝试以下方法:

var result = investigators.Where(inv=>NewInvestigators.Any(ninv=>ninv.id == inv.investigatorId))
                          .OrderBy(inv=>inv.id);
获得相同结果的另一种方法是使用
连接

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

是的,在Linq中使用join。显然,在Linq中使用join是可能的。使用joinno join,no common FEILD为什么不投票,这是有效的question@Downvoter我很感激你对否决票的推理。提前谢谢。这个解决方案是正确的,所以我看不出有人会反对投票的原因,我看到的唯一一点是OP也在寻找排序,这个解决方案的时间复杂度为O(N^2),可以改进。@MrinalKamboj谢谢。我还添加了顺序。我在两个列表中都没有确切的数据,只是想在调查者中我有Id,在新的调查者中我有Id。我需要匹配并排序。在NewInvestor中,我有一个对象,它有两个属性,InvestorID和Name。在调查员中,我有身份证、城市、国家
var result = from inv in investigators
             join ninv in NewInvestigators
             on inv.id equals ninv.investigatorId 
             order by inv.id
             select inv;