使用LINQ从其他两个列表中填充单个列表而不重复

时间:2012-09-21 08:01:42

标签: c# linq c#-4.0 linq-to-sql

我有2个名单: -

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" });

现在我希望合并第二个列表,如果可能的话,使用LINQ,具体取决于第一个列表。

例如,原始列表中只有一个橙色条目,我希望将“正常”列表中的2个条目附加到“原始”列表中。香蕉也一样。

如何使用LINQ实现这一目标?

感谢您的帮助和时间

------------结果我希望实现

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

5 个答案:

答案 0 :(得分:1)

试试这个:

        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);
        }

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果Fruit是struct,那么:

var result = OrigFruitList.Union(NormalFruitList);

如果Fruit是这样的,那么:

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);

答案 3 :(得分:0)

假设当另一个水果具有相同的Category SubCategory时水果是重复的,您可以将Enumerable.Union与自定义{{3}一起使用}}:

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();
        }
    }
}

现在您可以使用Union中的比较器:

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

答案 4 :(得分:0)

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();
  }

}