2

我有 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 合并。

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

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

感谢您的帮助和时间

------------RESULT 我希望实现

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

5 回答 5

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);
        }
于 2012-09-21T08:12:48.223 回答
0
var mrgList = OrigFruitList
    .Union(NormalFruitList)
    .GroupBy(n => new {n.Category, n.SubCategory}, (fruit, fruits) => fruits.First());
于 2012-09-21T08:03:52.997 回答
0

如果 Fruit 是这样的结构:

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);
于 2012-09-21T08:11:45.857 回答
0

假设当另一个水果具有相同的Category SubCategory时,一个水果是重复的,您可以使用Enumerable.Union自定义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();
        }
    }
}

现在您可以在以下位置使用比较器Union

OrigFruitList = OrigFruitList
                .Union(NormalFruitList, new Fruit.Comparer())
                .ToList();
于 2012-09-21T08:15:55.187 回答
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();
  }

}
于 2012-09-21T08:17:33.047 回答