-1

I have a list "A" and a generic list "B". I need to find all the items of "A" those are not in "B" with multiple condition.

List "A":

EXWORK
CENTAGES
PREMIUM

List "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. I am not figuring out where I am making mistake.

My Desired result is:

EXWORK
CENTAGES
sonu
  • 1
  • 2
  • Hi Sonu, Welcome to the stackoverflow. We would like to see [reprex], where you can try out and solve your query. It is really difficult for us to look at image and analyse Linq and identify problem in it. I would also encourage you to go through [how to ask](https://stackoverflow.com/help/how-to-ask) documentation which explains you how to ask a good question – Prasad Telkikar Feb 26 '22 at 17:46
  • You claim you want to find all items of `A` that are _not_ in `B`; and yet, you want both `EXWORK` and `CENTAGES` returned? That doesn't make sense to me. – Astrid E. Feb 26 '22 at 18:02
  • Sorry @AstridE. edited. – sonu Feb 26 '22 at 18:15
  • I still find it difficult to understand exactly what you need, but hopefully one of the provided answers will be something you can use? – Astrid E. Feb 26 '22 at 18:36
  • 1
    @sonu If you want `EXWORK` and `CENTAGES` why do you have the condition `b.CATAGORY == "SUPPLY"`? Could you try explain the rule you are looking for in words? – Xerillio Feb 26 '22 at 20:52
  • Perhaps what you want is something more like: `A.Where(a => B.Where(b => b.CATAGORY == "SUPPLY").All(b => b.PARTICULARS != a)` – NetMage Feb 28 '22 at 19:37

3 Answers3

1

Do you need to find all items in A that are not listed as a PARTICULAR categorized as SUPPLY in B?

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.

var value = A.Except(
    B.Where(b => b.CATAGORY == "SUPPLY")
        .Select(b => b.PARTICULARS));

Example fiddle here.

Astrid E.
  • 2,280
  • 2
  • 6
  • 17
  • I don't think `Except` is appropriate since `A` and `B` are not the same type. – NetMage Feb 28 '22 at 19:38
  • 1
    `A` and `B` are not the same type, but `A` and `B.Select(b => b.PARTICULARS)` are. Unless I've misinterpreted something. – Astrid E. Feb 28 '22 at 21:34
0

I think perhaps you mean:

var value = A.Where(a => !B.Any(b => b.CATAGORY == "SUPPLY" && b.PARTICULARS == a));
Xerillio
  • 4,855
  • 1
  • 17
  • 28
0

If you look at your query, you are trying to filter list A based on the condition from List B i.e b.CATAGORY == "SUPPLY" and PARTICULARS should be present in A list.

To get desire output, you have iterate though List B and filter records based on condition b.CATAGORY == "SUPPLY" and PARTICULARS property,

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,

var resultB = B
    .Where(x => x.CATAGORY == "SUPPLY")
    .Select(x => x.PARTICULARS).ToList();

var value = A.Except(resultB);
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44