简体   繁体   English

如何找到不在具有多个条件的通用列表中的列表的所有项目

[英]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中分类为SUPPLYPARTICULAR的项目?

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));

Example fiddle here .示例小提琴在这里

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.

Try Online在线试用


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.

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