简体   繁体   English

如何在LINQ中获得独特的价值

[英]how to get distinct value in linq

i have used below linq query to join some table to get accurate data.. 我已经在下面的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();

i have a small problem in CustomerDetail records i only want to get distinct records from the CustomerDetail based on the CustomerId.... 我在CustomerDetail记录中有一个小问题,我只想根据CustomerId从CustomerDetail中获得不同的记录。

CustomerDetail can have more than one records for the same CustomerId CustomerDetail可以为同一CustomerId拥有多个记录

please suggest how i can filter my query to get only distinct records from the CustomerDetail table 请建议我如何过滤查询以仅从CustomerDetail表中获取不同的记录

thanks, 谢谢,

You can implement a comparer class for your CustomerDetail(s)-Objects and then use the overloaded version, which accepts your comparer. 您可以为CustomerDetail(s)-Objects实现一个比较器类,然后使用接受您的比较器的重载版本。

Something like this: 像这样:

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());

LINQ provides the Distinct method. LINQ提供了Distinct方法。 However, it uses the default equality comparer by default, or optionally one you specified. 但是,它默认使用默认的相等比较器,也可以使用您指定的可选比较器。

I would recommend instead of using an anonymous type, define a class with the properties CustomerId and CustomerName, then override Equals. 我建议不要使用匿名类型,而是使用属性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();
   }
}

and then 接着

(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();

Just use .Distinct() before .ToList() Ie 只需在.ToList()之前使用.ToList()

(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();

尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM