SQL to LINQ with multiple join, count and left join

后端 未结 1 1990
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 05:18

I wrote this SQL request with multiple JOIN (including a LEFT JOIN).
It gives me the expected result.

SELECT         


        
相关标签:
1条回答
  • 2020-11-21 05:47

    For translating SQL to LINQ query comprehension:

    1. Translate subselects as separately declared variables.
    2. Translate each clause in LINQ clause order, translating monadic and aggregate operators (DISTINCT, TOP, MIN, MAX etc) into functions applied to the whole LINQ query.
    3. Use table aliases as range variables. Use column aliases as anonymous type field names.
    4. Use anonymous types (new { ... }) for multiple columns (e.g. in groupby).
    5. Use First().field to get non-key values from the groupby aggregate range variable.
    6. JOIN conditions that are multiple ANDed equality tests between the two tables should be translated into anonymous objects on each side of equals
    7. JOIN conditions that aren't all equality tests with AND must be handled using where clauses outside the join, or with cross product (from ... from ...) and then where. If you are doing LEFT JOIN, add a lambda Where clause between the join range variable and the DefaultIfEmpty() call.
    8. LEFT JOIN is simulated by using into joinvariable and doing another from the joinvariable followed by .DefaultIfEmpty().
    9. Replace COALESCE with the conditional operator (?:)and a null test.
    10. Translate IN to .Contains() and NOT IN to !...Contains(), using literal arrays or array variables for constant lists.
    11. Translate x BETWEEN low AND high to low <= x && x <= high.
    12. Translate CASE and IIF to the ternary conditional operator ?:.
    13. SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
    14. SELECT columns must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
    15. References to computed SELECT columns can be translated by repeating the expression or by using let to name the expression before its first use.
    16. Proper FULL OUTER JOIN must be handled with an extension method.
    17. Translate UNION to Concat unless both sub-queries are DISTINCT, in which case you can translate to Union and leave off the DISTINCT.
    18. Translate aggregate queries that have no GROUP BY using a singleton GroupBy: add .GroupBy(r => 1) and then translate the aggregate functions in the Select.
    19. Date Math and some other canonical functions can be accessed using EF.Functions to get an instance of the DbFunctions class (EF Core) or EntityFunctions to access the static methods (EntityFramework).

    Applying these rules to your SQL query, you get:

    var subrq = from r in Table_R
                group r by r.Id into rg
                select new { Id = rg.Key, cnt = rg.Count() };
    
    var ansq = (from c in Table_C
                join v in Table_V on c.Id equals v.Id
                join r in subrq on c.Id equals r.Id into rj
                from r in rj.DefaultIfEmpty()
                where c.IdUser == "1234"
                group new { c, v, r } by new { c.Id, c.Title, r.cnt } into cvrg
                select new {
                    cvrg.Key.Title,
                    Nb_V2 = cvrg.Count(),
                    Nb_V1 = cvrg.Select(cvr => cvr.v.IdUser).Distinct().Count(),
                    Nb_R = (int?)cvrg.Key.cnt
                }).Distinct();
    

    The lambda translation is tricky, but the conversion of LEFT JOIN to GroupJoin...SelectMany is what is needed:

    var subr2 = Table_R.GroupBy(r => r.Id).Select(rg => new { Id = rg.Key, cnt = rg.Count() });
    var ans2 = Table_C.Where(c => c.IdUser == "1234")
                      .Join(Table_V, c => c.Id, v => v.Id, (c, v) => new { c, v })
                      .GroupJoin(subr, cv => cv.c.Id, r => r.Id, (cv, rj) => new { cv.c, cv.v, rj })
                      .SelectMany(cvrj => cvrj.rj.DefaultIfEmpty(), (cvrj, r) => new { cvrj.c, cvrj.v, r })
                      .GroupBy(cvr => new { cvr.c.Id, cvr.c.Title, cvr.r.cnt })
                      .Select(cvrg => new { cvrg.Key.Title, Nb_V2 = cvrg.Count(), Nb_V1 = cvrg.Select(cvr => cvr.v.IdUser).Distinct().Count(), Nb_R = (int?)cvrg.Key.cnt });
    
    0 讨论(0)
提交回复
热议问题