Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET Unique List from different classes using LINQ

Tags:

c#

linq

I have 2 classes with different properties

ClassA
    int Id
    int Name
    int Status

ClassB
    int ClassAId
    string Bprop1
    string Bprop2
    string Bprop3
    ...

I have List of ClassA and List of ClassB and i want to get a unique list of ClassA values with the condition

List where ClassA.Id is present with List on property ClassAId

Example:

// given
List<ClassA>  {{1,"a"},{2,"b"},{3,"c"},{4,"d"}} etc
Lisst<ClassB>  {{1,"aaa","ccc","aasdaf"},{3,"aaa","ccc","aasdaf"}}

// expected result
List<ClassA>  {{1,"a"},{3,"c"}}  

How can I apply a LINQ query for the same?

like image 950
Sebastian Avatar asked Dec 02 '25 20:12

Sebastian


1 Answers

This should give you the expected output:-

List<ClassA> resultClassA = classAObj.Where(a => classBObj.Any(b => b.ClassAId == a.Id))
                                     .ToList();
like image 123
Rahul Singh Avatar answered Dec 04 '25 09:12

Rahul Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!