0

I am struggling in converting following SQL to LINQ. Can anyone help me please :

   
 SELECT M.Id, M.borrower_id, M.application_id,  [borrower_first_name] ,[borrower_last_name], DP.ID, DP.masterline_id, PR.ID, PR.masterline_id
  FROM [dbo].[CCMemberizationPII] AS M
         LEFT JOIN 
          (SELECT  MAX(DP.ID) ID, DP.masterline_id
          FROM [dbo].[CCDailyPositions] AS DP 
          GROUP BY DP.masterline_id) DP
       ON DP.masterline_id = M.application_id
        LEFT JOIN 
          (SELECT  MAX(PR.ID) ID, PR.masterline_id
          FROM [dbo].[CCPurchase] AS PR
          GROUP BY PR.masterline_id) PR
       ON PR.masterline_id = M.application_id 
  order by M.Id 
1
  • A good start would be to find an O/R mapper that supports LINQ. Commented Oct 26, 2021 at 6:52

1 Answer 1

1

When building LINQ query, try do decompose parts for building final query.

var positions = 
    from dp in ctx.CCDailyPositions
    group by new { dp.masterline_id } into g
    select new 
    {
        ID = g.Max(x => x.ID),
        dp.masterline_id
    };

var purchases = 
    from pr in ctx.CCPurchase
    group by new { pr.masterline_id } into g
    select new 
    {
        ID = g.Max(x => x.ID),
        pr.masterline_id
    };

var query = 
    from m in ctx.CCMemberizationPII
    from dp in positions.Where(dp => dp.masterline_id == m.application_id)
        .DefaultIfEmpty()
    from pr in purchases.Where(pr => pr.masterline_id == m.application_id)
        .DefaultIfEmpty()
    orderby m.Id
    select new 
    {
        m.Id, 
        m.borrower_id, 
        m.application_id,  
        m.borrower_first_name,
        m.borrower_last_name, 
        DP_ID = dp.ID, 
        PR_ID = pr.ID, 
    };

Also your query can be written using OUTER APPLY. Note that EF Core can transform OUTER APPLY to LEFT JOIN with Window Function usage.

var query = 
    from m in ctx.CCMemberizationPII
    from dp in ctx.CCDailyPositions.Where(dp => dp.masterline_id == m.application_id)
        .OrderByDescending(dp => dp.ID)
        .Take(1)
        .DefaultIfEmpty()
    from pr in ctx.CCPurchase.Where(pr => pr.masterline_id == m.application_id)
        .OrderByDescending(pr => pr.ID)
        .Take(1)
        .DefaultIfEmpty()
    orderby m.Id
    select new 
    {
        m.Id, 
        m.borrower_id, 
        m.application_id,  
        m.borrower_first_name,
        m.borrower_last_name, 
        DP_ID = dp.ID, 
        PR_ID = pr.ID, 
    };

Which SQL will be better, depends on execution plan in your particular case.

Sign up to request clarification or add additional context in comments.

3 Comments

I ended up using the OUTER APPLY. I am getting same results from SQL and LINQ in LINQPad. Can you please refer me to any site where I can learn about this OUTER APPLY. I can write SQL but finding it hard to translate it into LINQ. Any suggestions will be appreciated.
What exactly do you need? How to express OUTER APPLY by LINQ or what is OUTER APPLY?
How to express OUTER APPLY by LINQ!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.