如何在linq中获得不同的价值

时间:2012-05-22 05:44:01

标签: c# linq

我使用下面的linq查询加入一些表来获取准确的数据..

(from row in
  (from c in DbContext.Customer
   join cd in DbContext.CustomerDetails
   on c.Customer_Id equals cd.CustomerDetail_CustomerId
   join cp in DbContext.ProductPurchases
   on cd.CustomerDetail_OrgID equals cp.ProductPurchase_OrgID
   where cd.CustomerDetail_OrgId == OrganizationID --organization Id is common
   && c.Customer_Org_Id == OrganizationID
   && cp.ProductPurchase_OrgID == OrganizationID
   orderby cd.CustomerDetail_CreatedDate descending
   select new { c, cd, cp })
   select new CustomerDTO
   {
      CustomerId = row.cpd.CustomerDetail_CustomerID,
      CustomerName = row.c.Customer_LastName+", "+row.c.Customer_FirstName,
   }).ToList();

我在CustomerDetail记录中遇到一个小问题我只想根据CustomerId从CustomerDetail获取不同的记录....

CustomerDetail可以为同一CustomerId提供多条记录

请建议我如何过滤我的查询以仅从CustomerDetail表中获取不同的记录

感谢,

4 个答案:

答案 0 :(得分:5)

您可以为CustomerDetail实现比较器类 - 对象,然后使用重载版本,该版本接受您的比较器。

这样的事情:

class CustomerDetailsComparer: IEqualityComparer<CustomerDetail>
{
    public bool Equals(CustomerDetail x, CustomerDetail y)
    {

        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.CustomerId == y.CustomerId;
    }


    public int GetHashCode(CustomerDetail cd)  
    {
         // Do something here
    }  
}


// ...

DbContext.CustomerDetails.Distinct(new CustomerDetailsComparer());

答案 1 :(得分:2)

LINQ提供Distinct方法。但是,它默认使用默认的相等比较器,或者您指定的默认相等比较器。

我建议不要使用匿名类型,定义具有CustomerId和CustomerName属性的类,然后重写Equals。

public class DistinctCustomer
{
   public int CustomerId { get; set; }
   public string CustomerName { get; set; }

   public override bool Equals(object obj)
   {
        if (ReferenceEquals(obj, null)) return false;
        if (ReferenceEquals(this, obj)) return true;

        var other = obj as DistinctCustomer;

        if (other == null) return false;

        return CustomerId == other.CustomerId;
   }

   public override int GetHashCode()
   {
        return CustomerId.GetHashCode();
   }
}

然后

(from row in
  (from c in DbContext.Customer
   join cd in DbContext.CustomerDetails
   on c.Customer_Id equals cd.CustomerDetail_CustomerId
   join cp in DbContext.ProductPurchases
   on cd.CustomerDetail_OrgID equals cp.ProductPurchase_OrgID
   where cd.CustomerDetail_OrgId == OrganizationID --organization Id is common
   && c.Customer_Org_Id == OrganizationID
   && cp.ProductPurchase_OrgID == OrganizationID
   orderby cd.CustomerDetail_CreatedDate descending
   select new { c, cd, cp })
   select new DistinctCustomer
   {
      CustomerId = row.cpd.CustomerDetail_CustomerID,
      CustomerName = row.c.Customer_LastName+", "+row.c.Customer_FirstName,
   }).Distinct().ToList();

答案 2 :(得分:1)

.ToList()之前使用.Distinct() 即。

(from row in
  (from c in DbContext.Customer
   join cd in DbContext.CustomerDetails
   on c.Customer_Id equals cd.CustomerDetail_CustomerId
   join cp in DbContext.ProductPurchases
   on cd.CustomerDetail_OrgID equals cp.ProductPurchase_OrgID
   where cd.CustomerDetail_OrgId == OrganizationID --organization Id is common
   && c.Customer_Org_Id == OrganizationID
   && cp.ProductPurchase_OrgID == OrganizationID
   orderby cd.CustomerDetail_CreatedDate descending
   select new { c, cd, cp })
   select new 
   {
      CustomerId = row.cpd.CustomerDetail_CustomerID,
      CustomerName = row.c.Customer_LastName+", "+row.c.Customer_FirstName,
   }).Distinct().ToList();

答案 3 :(得分:0)

尝试:

(--your code--).Distinct().ToList();