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 を使用して 2 番目のリストをマージしたいと考えています。

たとえば、Original リストには Orange のエントリが 1 つしかなく、Normal リストの 2 つのエントリを Original リストに追加したいと考えています。バナナも同様です。

どうすれば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 同じand を持つ別の果物があるときに果物が重複していると仮定すると、カスタムでSubCategory使用できます。Enumerable.UnionIEqualityComparer<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 に答える