0

I have an object like this :

public class myObject
{
public string Name {get; set;}
public string ID {get; set;}
}

I have two lists like this

List<myObject> list1 = new List<myObject>();
list1.Add(new myObject(){Name = Jason, ID = 1});
list1.Add(new myObject(){Name = Jonathan, ID = 2});
list1.Add(new myObject(){Name = Kevin, ID = 3});

List<myObject> list2 = new List<myObject>();
list2.Add(new myObject(){Name = Jennifer, ID = 5});
list2.Add(new myObject(){Name = Samantha, ID = 2});
list2.Add(new myObject(){Name = Lucy, ID = 9});

I want to intersect these two lists by their IDs. I mean I want to get Jonathan's and Samantha's objects in another list. How can I do that? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198
  • People aren't here to do your work for you, although it seems some are, when asking questions be sure to be clear about what you are trying to do. It is also important to show what you have tried already by including it as a [MCVE] in the question itself. If you need to add extra information into your post later on you can always [edit] it in. – TheLethalCoder May 17 '17 at 10:24
  • Possible duplicate of [Intersect with a custom IEqualityComparer using Linq](http://stackoverflow.com/questions/4340273/intersect-with-a-custom-iequalitycomparer-using-linq) – Mark Rotteveel May 17 '17 at 16:23

2 Answers2

4

With Join for example

var query = from o1 in list1 
            join o2 in list2 on o1.ID equals o2.ID
            select new { Object1 = o1, OBject2 = o2 };

I don't know what kind of list you want as result because your class has only one property ID.

Maybe you want a Dictionary<int, List<myObject>> instead, the dictionary has the common ID as key and a list with the objects as value:

Dictionary<int, List<myObject>> result = list1.Concat(list2)
    .GroupBy(x => x.ID)
    .ToDictionary(g => g.Key, g => g.ToList());
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • this syntax is a bit oldschool isn't it? – Toshi May 17 '17 at 10:28
  • @Toshi not at all, what gives you that idea? Whether you prefer this or the method syntax (ala my answer) is simply a matter of preference. – Jamiec May 17 '17 at 10:28
  • @Toshi Even if it is why can't you write "old school" code? – TheLethalCoder May 17 '17 at 10:29
  • @Toshi: in what way oldschool? I'd always prefer query- over method-syntax when it comes to join. But i have never heard that query-syntax is oldschool. It's just syntactic sugar provided by the language to enable you to write more readable code. It gets translated to method syntax anyway. If you have to use methods that aren't supported by the query-synzax you can split your query into multiple parts. That makes it a lot more readable without performance loss due to deferred execution. – Tim Schmelter May 17 '17 at 10:33
  • `select new { Object1 = o1, OBject2 = o2 };` This part seems wrong. How can I pass o1 and o2 to another list? Thanks. – jason May 17 '17 at 14:19
  • @jason: what kind of other list? – Tim Schmelter May 17 '17 at 14:23
  • I mean there would be another list like this : `List list3 = new List();`. How can I push `o1` and `o2` to that list? – jason May 17 '17 at 14:25
  • @jason: it's easy, but then you have two independend `myObject` instances in this list with the same ID, what's with the other ID's? They will mix up again. You want a `List>`? Have you looked at my dictionary approach? That has the advantage that you search for ID's and you get a list of `myObject` instances that share this ID. – Tim Schmelter May 17 '17 at 14:30
  • Records with the same IDs is fine. That's actually what I want. Thanks. – jason May 17 '17 at 14:31
  • @jason: look at my dictionary approach – Tim Schmelter May 17 '17 at 14:31
4

You can do this with a Join

var result = list1.Join(list2, l1 => l1.ID, l2 => l2.ID, 
    (lhs,rhs) => new {ID = lhs.ID, Name1 = lhs.Name, Name2 = rhs.Name};
);

Live example: http://rextester.com/OAJRG62251

Jamiec
  • 133,658
  • 13
  • 134
  • 193