Question: I have two queries, each one returning a list of objects.

```
List<A> list1 = (....query...)
List<A> list2 = (....query...)

```

"A" is an object model.

Both queries return almost the same objects but with different properties set.

I want to remove duplicates merge them into a single list based on a property of object A.

Basically something like this: 

```
List<A> finalLis = list1 join list2 on elemList1.somePropID == elemList2.somePropID 

```

In simple C# style it would be something like this:

```
foreach(elem1 : list1) {
    foreach(elem2: list1) {
       if(elem1.someID == elem2.someID) {
           elem1.someProp = elem2.someProp
           elem1.otherProp = elem2.otherProp
        }
     }
}

```

I don't want to do it in this way because I'm sure there's a more elegant way in linq.

If you have any suggestions please let me know.

Answer: 
Linq can help you with selecting but not with updating. So you won't get rid of `foreach` statement. So your task could be written with linq like this:

```
//the query is like LEFT JOIN in SQL
var query = from x in list1
            join y in list2 on x.IDItem equals y.IDItem
            into z
            from q in z.DefaultIfEmpty()
            select new {IOne = x, ITwo = q};
foreach (var pair in query)
{
    if (pair.ITwo != null) // && pair.IOne.OneProperty != null
        pair.IOne.OneProperty = pair.ITwo.TwoProperty;
}

var resultList = query.Select(x => x.IOne).ToList();

```

You can check the results [here](http://ideone.com/ecXeXg).
It is right to want to use LINQ.

The answers here are all equivalent to the given C# snippet apart from that the snippet is very inefficient and will cause list1.Length \* list2.Length operations.

Using LINQ joins is much more efficient and results in roughly list1.Length + list2.Length operations.

This is because the join on the id will do something like this using a dictionary

```
// create a dict so that you can lookup things in item1 based on id
var list1IdToADict = list1.ToDictionary(a => a.id);

//filter out things not in both lists (join on there id)
var joinedList2 = list2.Where(a => list1IdToADict.ContainsKey(a.id)); 

```

Other answers are more elagant but if you wanted to do it this way you could finish with

```
foreach(var a in joinedList2){
    a.someProp = list1IdToADict[a.id].someProp;
    a.otherProp = list1IdToADict[a.id].otherProp;
}

```