Join two lists using linq queries - C#

15,115

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:

  1. Method chain:

    var joined = customers1.Union(customers2);
    
  2. 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.

Share:
15,115

Related videos on Youtube

ThisaruG
Author by

ThisaruG

Human

Updated on July 18, 2022

Comments

  • ThisaruG
    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 named customers1 and customers2. I need to join these two without using Collections methods (like customer1.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
      Yeldar Kurmangaliyev over 8 years
      It looks like there is no query equivalent for Union method. What is the reason of not using Union method?
    • Corak
      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
      Corak over 8 years
      Yes, 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
    ThisaruG over 8 years
    I 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
    Kosala W over 8 years
    This 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
    ThisaruG over 8 years
    As I mentioned, I need to join two Lists without repetitive elements. It's a part of an assignment !
  • Kosala W
    Kosala W over 8 years
    Then you have to Union to combine two collections and then Distinct to filter out duplicates. Or you can use Intersect, then Union and then Except. There are many ways to do it.
  • ThisaruG
    ThisaruG over 8 years
    Thank you for the help. I tried and succeed the method provided by @yeldarKurmangaliyev