1

I have a SQL query which I am performing on two tables.

Here is my query

Select a.cSubGroupName,a.cAddedBy,a.dAddedOn,b.cGroupName 
from sys_Log_Account_SubGroup a,sys_Account_Primary_Group b 
where a.cAuthorizedStatus='Pending'  and a.nGroupCode=b.nGroupCode 

I am trying to perform the same query using Lambda expression.

1 Answer 1

2
var query = tA
    .Where(a => a.cAuthorizedStatus == "Pending")
    .Join(tB, a => a.nGroupCode, b => b.nGroupCode, (a, b) => new 
    { 
        cSubGroupName = a.cSubGroupName, 
        cAddedBy = a.cAddedBy, 
        dAddedOn = a.dAddedOn, 
        cGroupName = b.cGroupName 
    });

or

var query = tA
    .Join(tB, a => a.nGroupCode, b => b.nGroupCode, (a, b) => new 
    { 
        cSubGroupName = a.cSubGroupName, 
        cAddedBy = a.cAddedBy, 
        dAddedOn = a.dAddedOn, 
        cGroupName = b.cGroupName,
        cAuthorizedStatus = a.cAuthorizedStatus
    })
    .Where(j => j.cAuthorizedStatus == "Pending")
    .Select(j => new
    { 
        cSubGroupName = j.cSubGroupName, 
        cAddedBy = j.cAddedBy, 
        dAddedOn = j.dAddedOn, 
        cGroupName = j.cGroupName
    });

Note that the last select is only necessary if you want to explicity exclude the cAuthorizedStatus column in the return set.

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

2 Comments

Thanks for the answer, trying to understand the expression ,thanks.
@freebird. The .Join method takes: 1. set to (inner) join on 2. key to join left set on 3. key to join right set on 4. output set (column list).

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.