简体   繁体   中英

sorting by using linq comparing two lists?

I have 2 list of items;

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.

     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. If Id matches get the particular investigator(basically a sort on investigators based on Id existing in NewInvesigators).

I can do it by using for each, but I want to know whether it is possible with linq ?

in newinvestigator I have object which is having two property, investigatorId and Name.

In Investigator I have property Id , City , country. no Name or investigatorId in the investigator list.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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