gpt4 book ai didi

c# - 如何在linq中获得不同的值(value)

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:18 26 4
gpt4 key购买 nike

我已经使用下面的 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 中获取不同的记录....

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

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

谢谢,

最佳答案

您可以为您的 CustomerDetail(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());

关于c# - 如何在linq中获得不同的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com