如何获得一个不在另一个列表中的项目列表 [英] How to Attain a list of items of items not on another list

查看:100
本文介绍了如何获得一个不在另一个列表中的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从名称不在第二个列表中的列表中获取BeerNames的列表.

I want to attain a list of BeerNames from a list whose names are not on a second list.

var myList=(from f in Beers where ...
              select f.BeerNames).ToList

var storeList(from f in Store where...
                select f).ToList()

MyList将具有一些不在StoreList上的BeerName.我如何找到那些BeerName?我尝试了.Except!Contains,但是我意识到我做错了事,并且缺少一些关键知识. 谢谢

MyList will have some BeerNames that are not on StoreList. How do I find those BeerNames? I tried .Except and !Contains but I realize I'm doing something wrong, and missing some key bit of knowledge. thanks

如果我更改

var storeList(from f in Store where...
                select f).ToList()

var storeList(from f in Store where...
                select f.BeerNames).ToList()

然后我可以使用除List1.Except(List2)之类的东西.我不确定是否有更好的方法.抱歉,如果不清楚,我正在尝试:)

then I can use except such as List1.Except(List2). I wasn't sure if there was a better way. Sorry folks if this isn't clear...I'm trying:)

推荐答案

var list1 = new String[] {"ABC1","ABC2", "ABC3", "ABC4"} ;
var list2 = new String[] {"ABC3","ABC4", "ABC5", "ABC6"} ;
var list3 = list1.Except(list2); // list3 holds ABC1, ABC2

除了工作正常之外.

我怀疑是linq查询返回的项目中有问题. 似乎像第一个中的f.BeerNames和StoreList中的f都没有指向相同的数据类型.

I suspect issue is in the item returned from the linq query. Seems like f.BeerNames in first, and f in StoreList are not pointing to same datatype.

对于异构类型

var list1 = from s in new String[] {"ABC1","ABC2", "ABC3", "ABC4"} select new {BeerName=s,Id = Guid.NewGuid().ToString()}  ;
var list2 = new String[] {"ABC3","ABC4", "ABC5", "ABC6"} ;
var intermediateList = list1.Select(i=>i.BeerName).Except(list2);
var list3 = from l1 in list1
        join l2 in intermediateList on l1.BeerName equals l2
        select l1;


list1.Dump(); // run in linqPad
intermediateList.Dump();// run in linqPad
list3.Dump();// run in linqPad

list3返回以下内容

list3 returns following

啤酒名称ID

ABC1 569ace9a-66c4-46aa-bbf5-50d586a2886f

ABC1 569ace9a-66c4-46aa-bbf5-50d586a2886f

ABC2 af456094-9692-4771-b489-8b3cca8aa938

ABC2 af456094-9692-4771-b489-8b3cca8aa938

使用 LinqPad 来运行上述命令,或删除.Dump()以在VS中执行.

Use LinqPad to run the above, or remove .Dump() to execute in VS.

这篇关于如何获得一个不在另一个列表中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆