0

I have two list of the same type of object (X)

X has this properties:

  • Id: int
  • Name: string
  • Month: string
  • ISSPA: string

I want to get the items which has the same value in the properties Month and ISSPA. For example:

List 1

Item 1 
{
   Id = 1, 
   Name = "John",
   Month = "October"
   ISSPA = "1234"
}

Item 2
{
   Id = 2, 
   Name = "Ryan",
   Month = "September"
   ISSPA = "1234"
}

List 2

Item 1 
{
   Id = 1, 
   Name = "Chris",
   Month = "September"
   ISSPA = "1234"
}

In this case I need to get Item 2 (List1) and Item 1 (List2). I tried a lot of things to get something decent but all failed.

iMSn20
  • 255
  • 2
  • 11
  • 1
    Could you share your code please. At least show what you tried, we can start from there – Legit007 Nov 24 '22 at 12:16
  • [https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins](https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins) – vernou Nov 24 '22 at 12:17
  • What's the difference between List 1 and List 2? Could the Item 1 from List 2 be added to List 1 instead? – Steven Nov 24 '22 at 12:18
  • Does this answer your question? [How to do joins in LINQ on multiple fields in single join](https://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join) – vernou Nov 24 '22 at 12:18

2 Answers2

1
var result = list1
                .Concat(list2)
                .GroupBy(x => (x.Month, x.ISSPA))
                .Where(g => g.Count() > 1)
                .SelectMany(g => g);
Magnus
  • 45,362
  • 8
  • 80
  • 118
0

You can use LINQ to search for these items in your lists. For example:

var filteredList = yourList.Where(x => x.Month == "September" && x.ISSPA == "1234").ToList();

Simply do the same thing for your second list and combine both results into a new list.

Marvin Klein
  • 1,436
  • 10
  • 33