簡體   English   中英

通過使用linq比較兩個列表進行排序?

[英]sorting by using linq comparing two lists?

我有2個項目清單;

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

var NewInvestigators = base.ActivePage.Investigators;

我在調查員中有30個項目,在NewInvesigators中有20個項目,都具有屬性ID,InvId,我需要對此進行匹配。

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

我嘗試了此操作,但是沒有用,我想根據這兩個列表的匹配ID創建一個新列表。 如果Id匹配,則獲取特定的調查員(基本上是根據NewInvesigators中現有的ID對調查員進行排序)。

我可以通過為每個使用來做到這一點,但是我想知道linq是否可行?

在newinvestigator中,我有一個對象,它具有兩個屬性,investigatorId和Name。

在調查器中,我具有屬性ID,城市,國家。 調查者列表中沒有Name或researchigatorId。

您可以嘗試這樣的事情:

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

獲得相同結果的另一種方法是使用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