简体   繁体   English

C# 实体框架:Linq 在 GrandChildren 上过滤并在具有属性的父级上执行 Select

[英]C# Entity Framework: Linq Filter on GrandChildren and Conduct a Select on the Parent with Attributes

Our company is currently using Entity Framework Net Core 2.2 with Sql Server我们公司目前正在使用 Entity Framework Net Core 2.2 和 Sql 服务器

Trying to find all Distinct customers who purchased a certain Product Input Parameter.试图找到所有购买了某个产品输入参数的不同客户。 The following EF Linq query was written to get the distinct Customers.编写了以下 EF Linq 查询以获取不同的客户。

Later another question came up, how do we get more (navigation) properties of customer?后来另一个问题出现了,我们如何获得客户的更多(导航)属性? Should Include be placed Before the Where or After the Where? Include应该放在Where之前还是之后? Does it matter?有关系吗? When running the SQL Profiler, it noted no difference in the queries.运行 SQL Profiler 时,它注意到查询没有区别。 I just wanted to be sure in some cases does the location of Include here matter?我只是想确定在某些情况下 Include here 的位置是否重要?

select distinct c.customerName
from dbo.customer customer
inner join dbo.Transactions transaction
    on transaction.customerid = customer.customerid
inner join dbo.Purchases purchases
    on purchases.PurchaseId = transaction.PurchaseId
inner join dbo.Product product 
    on transaction.ProductId = product.ProductId
where tra.BKProduct = @ProductInput

Original Solution: C# Entity Framework: Linq Filter on GrandChildren and Conduct a Select on the Parent原始解决方案: C# 实体框架:Linq 过滤 GrandChildren 并在 Parent 上执行 Select

var customerData = db.Customer
                      .Where(p => p.Transactions.SelectMany(c => c.Purchases).Select(gc => gc.Product.Where(gc => gc.BKProduct == ProductInput).Any());

Another question came up, we need to get more (navigation) properties of Customer.另一个问题出现了,我们需要获取更多的客户(导航)属性。

Alternative 1:备选方案 1:

var customerData = db.Customer
                      .Include(c=>c.CustomerType)
                      .Include(c=>c.CustomerAddress)
                      .Where(p => p.Transactions.SelectMany(c => c.Purchases).Select(gc => gc.Product.Where(gc => gc.BKProduct == ProductInput).Any());

Alternative 2:备选方案 2:

var customerData = db.Customer
                      .Where(p => p.Transactions.SelectMany(c => c.Purchases).Select(gc => gc.Product.Where(gc => gc.BKProduct == ProductInput).Any())
                      .Include(c=>c.CustomerType)
                      .Include(c=>c.CustomerAddress)

In this specific case, it probably does not matter.在这种特定情况下,它可能无关紧要。

Since c# "Include" is about the SELECT and the JOIN of the generated sql.由于 c# “包含”是关于 SELECT 和生成的 sql 的 JOIN。

However, you do not want to use the "it does not matter" as a blanket statement.但是,您不想将“没关系”用作笼统的陈述。

See here answer below (and overall question and other answers).请参阅下面的答案(以及总体问题和其他答案)。

Does the order of LINQ functions matter? LINQ 功能的顺序是否重要?

When you start putting in things like Where and OrderBy, the order-of-the-operations can matter.当您开始输入诸如 Where 和 OrderBy之类的内容时,操作顺序可能很重要。

Always look at the generated sql and ask yourself "does it look reasonable"?总是查看生成的 sql 并问自己“它看起来是否合理”? (Which you already did from your question:)..I mention primarily this for future readers) (你已经从你的问题中做了:)..我主要为未来的读者提到这一点)

So in this specific case, it is a preference.因此,在这种特定情况下,这是一种偏好。 I typically put .Where last.我通常把.Where最后。 So your first example would match my personal preference.因此,您的第一个示例将符合我的个人喜好。

And for some "further investigation", check out something like: https://weblogs.asp.net/dixin/introducing-linq-3-waht-is-functional-programming对于一些“进一步调查”,请查看以下内容: https://weblogs.asp.net/dixin/introducing-linq-3-waht-is-functional-programming

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

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