これはあなたを助ける必要があります - それは、コンソールアプリケーションです...
using System;
using System.Collections.Generic;
using System.Linq;
namespace SO
{
class Program
{
public static void Main(string[] args)
{
var listA = new List<Product>();
var listB = new List<Product>();
listA.Add(new Product() {Id = 1, Name = "Hair Curling Tongs"});
listA.Add(new Product() {Id = 2, Name = "Toys"});
listA.Add(new Product() {Id = 3, Name = "Coffee"});
listB.Add(new Product() {Id = 3, Name = "Dress"});
listB.Add(new Product() {Id = 4, Name = "Handbag"});
var results = listA.Union(listB).Distinct(new EqualityComparer()).ToList();
foreach(var result in results)
{
Console.WriteLine("{0} ----- {1}", result.Id, result.Name);
}
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
class Product{
public int Id{get; set;}
public string Name {get; set;}
}
class EqualityComparer: IEqualityComparer<Product> {
public bool Equals(Product p1, Product p2) {
return p1.Id == p2.Id;
}
public int GetHashCode(Product p){
return p.Id;
}
}
}
これは同様にあなたのための有用なリンクのように見える - 私はこれを行うことができます知っていますhttp://www.elevenwinds.com/blog/linq-distinctby-with-lambda-expression-parameter/
ですが、idので、私が比較するために使用している唯一のものは別の方法ですか?私はちょうどIEqualityComparerがこの場合には残虐であるように感じる。 –
私はこのクラスを作るのが少し難しいと感じていますが、単純な関数で十分でしょう。しかし、 'Where'を' Contains'と組み合わせると複雑さが増し、アルゴリズムが遅くなります。 – Marnix
私はあなたが私に与えたドキュメントのように、私のProductオブジェクトにIEqualityComparerを実装することに決めました。これは、私がIDを比較するだけではなく、それ以上をチェックする必要があることがわかったからです。今これでユニオンをどうやって使うのですか? –