如何在 LINQ 中获取不同的值

本文关键字:获取 LINQ | 更新日期: 2023-09-27 18:35:06

我在下面使用了linq query来连接一些表以获得准确的数据。

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

我在客户详细信息记录中有一个小问题,我只想根据客户 ID 从客户详细信息中获取不同的记录....

CustomerDetail 可以有多个记录用于同一 CustomerId

请建议我如何过滤查询以仅从客户详细信息表中获取不同的记录

谢谢

如何在 LINQ 中获取不同的值

您可以为 CustomerDetails(s)-Objects 实现一个比较器类,然后使用接受比较器的重载版本。

像这样:

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 提供了 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();

只需使用 ..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();