JMon
JMon

Reputation: 3447

Use LINQ to populate a single list from two other lists without duplicates

I have 2 lists :-

OrigFruitList.Add(new Fruit { Category = "Apple", SubCategory = "" });
        OrigFruitList.Add(new Fruit { Category = "Apple", SubCategory = "Red Apple" });
        OrigFruitList.Add(new Fruit { Category = "Apple", SubCategory = "Green Apple" });
        OrigFruitList.Add(new Fruit { Category = "Orange", SubCategory = "" });
        OrigFruitList.Add(new Fruit { Category = "Peach", SubCategory = "" });
        OrigFruitList.Add(new Fruit { Category = "Grapes", SubCategory = "Green Grapes" });
        OrigFruitList.Add(new Fruit { Category = "Grapes", SubCategory = "Black Grapes" });
        OrigFruitList.Add(new Fruit { Category = "Bananas", SubCategory = "" });

        NormalFruitList.Add(new Fruit { Category = "Apple", SubCategory = "" });
        NormalFruitList.Add(new Fruit { Category = "Apple", SubCategory = "Red Apple" });
        NormalFruitList.Add(new Fruit { Category = "Apple", SubCategory = "Green Apple" });
        NormalFruitList.Add(new Fruit { Category = "Orange", SubCategory = "Blood Orange" });
        NormalFruitList.Add(new Fruit { Category = "Orange", SubCategory = "Sweet Orange" });
        NormalFruitList.Add(new Fruit { Category = "Peach", SubCategory = "" });
        NormalFruitList.Add(new Fruit { Category = "Bananas", SubCategory = "" });
        NormalFruitList.Add(new Fruit { Category = "Bananas", SubCategory = "Yellow Bananas" });
        NormalFruitList.Add(new Fruit { Category = "Bananas", SubCategory = "Green Bananas" });

Now I wish to merge the second list, with LINQ if possible, depending on the first list.

For example there is only 1 entry for Orange in the Original List, and I wish to append the 2 entries from the Normal list to the Original list. Same goes for Bananas.

How can I achieve that with LINQ?

Thanks for your help and time

------------RESULT I wish to achieve

//FinalResult
        //Apple
        //Red Apple
        //Green Apple
        //Orange
        //Blood Orange
        //Sweet Orange
        //Peach
        //Green Grapes
        //Black Grapes
        //Bananas
        //Yellow Banans
        //Green Bananas

Upvotes: 2

Views: 208

Answers (5)

Ivan Golović
Ivan Golović

Reputation: 8832

Try this:

        var difference = NormalFruitList.Where(normFruit =>
            !OrigFruitList.Exists(
                origFruit => origFruit.Category == normFruit.Category 
                    && origFruit.SubCategory == normFruit.SubCategory));

        // If new Category is found in NormalFruitList it will be added to the end
        int index = 0;
        var result = new List<Fruit>(OrigFruitList);
        foreach (var item in difference.Reverse())
        {
            index = result.IndexOf(OrigFruitList.FirstOrDefault(fruit => fruit.Category == item.Category));
            result.Insert(index == -1 ? OrigFruitList.Count : index + 1, item);
        }

Upvotes: 1

Zen
Zen

Reputation: 244

var result = OrigFruitList.Union(NormalFruitList,new FruitComparer())

public class FruitComparer : IEqualityComparer<Fruit>
{       

  public bool Equals(Fruit x, Fruit y)
  {
      return y.Category == y.Category && x.SubCategory == y.SubCategory;
  }

  public int GetHashCode(Fruit f)
  {
    return (f.Category + f.SubCategory).GetHashCode();
  }

}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460048

Assuming that a fruit is a duplicate when there's another fruit with the same Category and SubCategory, you can use Enumerable.Union with a custom IEqualityComparer<Fruit>:

class Fruit
{
    public String Category { get; set; }
    public String SubCategory { get; set; }

    public class Comparer : IEqualityComparer<Fruit>
    {
        public bool Equals(Fruit x, Fruit y)
        {
            return y.Category == y.Category && x.SubCategory == y.SubCategory;
        }

        public int GetHashCode(Fruit obj)
        {
            return (obj.Category + obj.SubCategory).GetHashCode();
        }
    }
}

Now you can use the comparer in Union:

OrigFruitList = OrigFruitList
                .Union(NormalFruitList, new Fruit.Comparer())
                .ToList();

Upvotes: 0

Malmi
Malmi

Reputation: 387

var mrgList = OrigFruitList
    .Union(NormalFruitList)
    .GroupBy(n => new {n.Category, n.SubCategory}, (fruit, fruits) => fruits.First());

Upvotes: 0

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

If Fruit is struct so:

var result = OrigFruitList.Union(NormalFruitList);

If Fruit is class so:

var resul=new List<Fruit>();
foreach(var fruit in NormalFruitList)
{
    var item = OrigFruitList.firstOrDefault(p=>p.Category == fruit.Category 
                             && p.SubCategory == fruit.SubCategory));
   if(item!=null)
       resul.Add(item);
}
NormalFruitList.AddRange(result);

Upvotes: 0

Related Questions