ayilmaz February 2016

need to do a subquery in Linq

I am trying to write the Linq version of the SQL script below. But, I couldnt do it. I couldnt do the SUBQUERY part. T_PROJECT and T_SERVICE_TRANSACTION tables are linked with each other by PROJECT_ID and P.ID, it is a foreign key. How can I write the Linq version of it?

SELECT P.PROJECT_NAME, P.TOTAL_TIME, 
   (
        P.TOTAL_TIME - ( SELECT SUM(GIVEN_SERVICE_TIME)  
                         FROM T_SERVICE_TRANSACTION 
                         WHERE PROJECT_ID=P.ID )
   ) 
FROM T_PROJECT P 

Answers


Kelly Gendron February 2016

Try something along these lines..

var result = Project.Select(p => new {
    ProjectName = p.ProjectName, 
    TotalTime = p.TotalTime, 
    CustomColumn = p.TotalTime - p.T_Service_Transactions
       .Sum( t=> t.GivenServiceTime)
});

Post Status

Asked in February 2016
Viewed 1,597 times
Voted 6
Answered 1 times

Search




Leave an answer