How would I improve this 7 line Linq Query that acts as a Specification?

家住魔仙堡 提交于 2019-12-07 15:58:53

问题


BigQuery at the top gets a set of Products and nested related tables. Then, I apply filtering in a poor attempt at a specification pattern. This is the filter code. There are three tables in the query, and I want to filter the top query by the value nested in the bottom query. Like I said, this currently produces the results we want.

However, the .Contains() produces a SQL WHERE EXISTS() clause for each. We really only need one, but I don't know how to get the inner ID to compare with the outer ID.

from p in bigQuery                            // Root table
where ( from pp in p.LPP                      // Level 1 nested table
        where (from pv in pp.LPV              // Level 2 nested table 
               where pv.colorid == intValue   // Our filter value
               select p.id).Contains(p.id)    // Where exists
        select p.id).Contains(p.id)           // Where exists
select p;

Any thoughts? This produces a 900 line SQL statement as-is, and we only have one filter so far.


回答1:


from p in bigQuery
where p.LPP.SelectMany(pv => pv.LVP).Any(x => x.colorid == intValue)
select p;

From what I can see, the above should be equivalent. Please try it and inspect the SQL generated, if working. Any case, it should not be far off.



来源:https://stackoverflow.com/questions/4084102/how-would-i-improve-this-7-line-linq-query-that-acts-as-a-specification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!