[英]How find all the items of a list which are not in a generic list with multiple condition
I have a list "A" and a generic list "B".我有一个列表“A”和一个通用列表“B”。 I need to find all the items of "A" those are not in "B" with multiple condition.我需要找到“A”中所有不在“B”中的具有多种条件的项目。
List "A":列表“A”:
EXWORK
CENTAGES
PREMIUM
List "B":列表“B”:
PARTICULARS CATAGORY DETAIL
EXWORK ERECTION ABC
CENTAGES ERECTION ABC
PREMIUM SUPPLY ABC
For this I use following code:为此,我使用以下代码:
var value = A.Where(a => B.All(b => b.CATAGORY == "SUPPLY" && b.PARTICULARS!=a));
but this return the value "Premium" also whereas it shouldn't be.但这也返回值“Premium”,而它不应该是。 I am not figuring out where I am making mistake.我不知道我在哪里犯了错误。
My Desired result is:我想要的结果是:
EXWORK
CENTAGES
Do you need to find all items in A
that are not listed as a PARTICULAR
categorized as SUPPLY
in B
?您是否需要在A
中找到所有未列为在B
中分类为SUPPLY
的PARTICULAR
的项目?
If so, you could find all items in B
where CATEGORY = "SUPPLY"
and return list A
except the PARTICULAR
values of the filtered B
items.如果是这样,您可以在B
中找到所有项目,其中CATEGORY = "SUPPLY"
并返回列表A
,但过滤B
项目的PARTICULAR
值除外。
var value = A.Except(
B.Where(b => b.CATAGORY == "SUPPLY")
.Select(b => b.PARTICULARS));
I think perhaps you mean:我想也许你的意思是:
var value = A.Where(a => !B.Any(b => b.CATAGORY == "SUPPLY" && b.PARTICULARS == a));
If you look at your query, you are trying to filter list A
based on the condition from List B
ie b.CATAGORY == "SUPPLY"
and PARTICULARS
should be present in A
list.如果您查看您的查询,您正在尝试根据列表B
中的条件过滤列表A
,即b.CATAGORY == "SUPPLY"
并且PARTICULARS
应该出现在A
列表中。
To get desire output, you have iterate though List B
and filter records based on condition b.CATAGORY == "SUPPLY"
and PARTICULARS
property,为了获得愿望 output,您已经遍历列表B
并根据条件b.CATAGORY == "SUPPLY"
和PARTICULARS
属性过滤记录,
var result = B
.Where(x => x.CATAGORY == "ERECTION" && A.Contains(x.PARTICULARS)) //Use Contains.
.Select(x => x.PARTICULARS); //Select only PARTICULARS value.
If you want data from List A
, then you can break your linq in two steps,如果你想要 List A
中的数据,那么你可以分两步打破你的 linq ,
var resultB = B
.Where(x => x.CATAGORY == "SUPPLY")
.Select(x => x.PARTICULARS).ToList();
var value = A.Except(resultB);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.