Join two lists using linq queries - C#
Solution 1
It looks like there is no query equivalent for Union
method. You will need to use this method either in method chain call or in your query.
If you look at MSDN documentation on returning the set union of two sequences, you will see the following official query:
var infoQuery =
(from cust in db.Customers
select cust.Country)
.Union
(from emp in db.Employees
select emp.Country)
;
So, there are only two options in your case:
-
Method chain:
var joined = customers1.Union(customers2);
-
LINQ query
var joined = (from c1 in customers1 select c1) .Union (from c2 in customers2 select c2);
Solution 2
Why not use Distinct to filter out the duplicates?
var joined = (from c1 in customers1
join c2 in customers2
on c1.CusIndex equals c2.CusIndex
select new {c1, c2}).Distinct();
There is a nice extension in Microsoft.Ajax.Utilities
. It has a function called DistinctBy
, which may be more relevant in your case.
Related videos on Youtube
Comments
-
ThisaruG almost 2 years
I have an assignment where I have to join two lists of the same type (Customer). They have similar entries which I have to avoid repetitions.
This is my customer class:
class Customer { private String _fName, _lName; private int _age, _cusIndex; private float _expenses; public Customer(String fName, String lName, int age, float expenses, int cusIndex) { this._fName = fName; this._lName = lName; this._age = age; this._expenses = expenses; this._cusIndex = cusIndex; } }
So I have two
List<Customer>
s namedcustomers1
andcustomers2
. I need to join these two without using Collections methods (likecustomer1.Union(customer2).ToList();
But using Linq queries.Here's the Linq query I wrote:
var joined = (from c1 in customers1 join c2 in customers2 on c1.CusIndex equals c2.CusIndex select new {c1, c2});
But this gives me the member who appear on both of the lists. But I need all, without repetition. Are there any solution ???
-
Yeldar Kurmangaliyev over 8 yearsIt looks like there is no query equivalent for
Union
method. What is the reason of not usingUnion
method? -
Corak over 8 years@ThisaruGuruge - Concat basically just enumerates the whole first enumerable and then the whole second enumerable. Distinct then basically skips all elements that have already been yielded. That's giving you, what you want: everyone (from both lists), but only once. But since Union seems to work for you (I guess it does the same under the hood), use that. :)
-
Corak over 8 yearsYes, according to referencesource it does. Relevant implementation (look for
UnionIterator
):Set<TSource> set = new Set<TSource>(comparer); foreach (TSource element in first) if (set.Add(element)) yield return element; foreach (TSource element in second) if (set.Add(element)) yield return element;
-
-
ThisaruG over 8 yearsI used this. But when I tried to convert it into a new List using
ToList()
method, It gives me Error.cannot implicitly convert type
-
Kosala W over 8 yearsThis is similar to a self join. ToList() will give you an anonymous collection. I am confused about what you are trying to achieve. You cannot cast an anonymous type to a known type.
-
ThisaruG over 8 yearsAs I mentioned, I need to join two Lists without repetitive elements. It's a part of an assignment !
-
Kosala W over 8 yearsThen you have to
Union
to combine two collections and thenDistinct
to filter out duplicates. Or you can useIntersect
, thenUnion
and thenExcept
. There are many ways to do it. -
ThisaruG over 8 yearsThank you for the help. I tried and succeed the method provided by @yeldarKurmangaliyev