开发者

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

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜