1
は、私は私はいくつかのテナントを持つテーブルを持っており、そのうちのいくつかは、同じ名前を持つ、今私のオブジェクトTenant
LINQではなぜこのケースでは別のものが動作しないのですか?
public class TenantComparer : IEqualityComparer<Tenant>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(Tenant x, Tenant y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.Name == y.Name;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(Tenant tenant)
{
//Check whether the object is null
if (Object.ReferenceEquals(tenant, null)) return 0;
//Get hash code for the Name field if it is not null.
int hashProductName = tenant.Name == null ? 0 : tenant.Name.GetHashCode();
//Calculate the hash code for the product.
return hashProductName;
}
}
ため、この比較子を持っています。私は間違っているところ
あなたが私に教えてくださいすることができます...
public static List<Tenant> GetTenantListOrderyByNameASC()
{
DataClassesDataContext db = new DataClassesDataContext();
var tenantsList = (from t in db.Tenants
select t).Distinct().OrderBy(x => x.Name).ToList();
return tenantsList;
}
しかし、それはまだ同じ名前のテナントを示しています。私は名前で重複しない注文している人を取得したいですか?あなたは、現時点ではあなたがそうでない、明示的に比較子を提供する必要が
ありがとうございました。また、返すことができるいくつかのトリックが 'List'である必要があります:var tenantsList =(from t。db.Tenants select t).OrderBy(x => x.Name).ToList()Distinct(new TenantComparer ())。ToList(); ' - ToList()を2回使用するのは奇妙に思えますが、そうでなければ'クエリの演算子 'Distinct'にサポートされていないオーバーロードが使用されます。 –