2012-09-21 127 views
1

我試圖將下面的SQL(不是我自己寫的)轉換成Linq到實體的查詢。將SQL轉換爲LINQ到實體

select 
    u.user_Id, 
    u.forename, 
    u.surname, 
    u.client_code, 
    u.user_name, 
    u.password, 
    u.email, 
    u.gender, 
    u.Report_Date, 
    u.EmailDate, 
    count(ut.test_Id) as testcount, 
    sum(cast(isnull(ut.completed,0) as int)) as Testcompleted, 
    u.job_function, 
    lu.lookupvalue 
from 
users u inner join user_Relationship ur 
    on u.user_Id= ur.child_Id  
left join user_tests ut 
    on ut.user_id=u.user_id 
inner join lookup lu on u.first_languageId = lu.lookupid 
where ur.parent_Id = @Parent_Id 
group by 
    u.user_Id, u.forename,u.surname,u.client_code,u.user_name, u.password, 
    u.email, u.gender, u.first_languageId, u.Report_Date,u.EmailDate, 
    u.job_function, lu.lookupvalue 

到目前爲止,我已經能夠做到這一點:

from u in db.Users 

join ur in db.User_Relationship on u.User_ID equals ur.Child_ID 

join ut in db.User_Tests on u.User_ID equals ut.User_ID into ps 
from ut in ps.DefaultIfEmpty() 

join lu in db.Lookups on u.First_LanguageID equals lu.LookupID 

where ur.Parent_ID == 45875 

select new UserViewModel 
{ 
    User_ID = u.User_ID, 
    Forename = u.Forename, 
    Surname = u.Surname, 
    Client_Code = u.Client_Code, 
    User_Name = u.User_Name, 
    Password = u.Password, 
    Email = u.Email, 
    Gender = u.Gender, 
    Report_Date = u.Report_date, 
    Email_Date = u.EmailDate, 
    //Insert Test_Count and Test_Completed 
    Job_Function = u.Job_Function, 
    Lookup_Value = lu.LookupValue 
}); 

如何複製SQL的GroupCount()功能?

+0

如果它帶你永遠做。所以爲什麼? – marko

回答

0

對此解決方案進行調整,因爲我目前沒有.Net環境來測試此解決方案,但您肯定會知道如何在linq中完成grouping

from u in db.Users 

join ur in db.User_Relationship on u.User_ID equals ur.Child_ID 

join ut in db.User_Tests on u.User_ID equals ut.User_ID into ps 
from ut in ps.DefaultIfEmpty() 

join lu in db.Lookups on u.First_LanguageID equals lu.LookupID 

where ur.Parent_ID == 45875 group new{u,lu} by new {u.User_ID,u.Forename, 
          u.Surname,u.Client_Code, 
          u.User_Name,u.Password, 
          u.Email,u.Gender,u.Report_date, 
          u.EmailDate,u.Job_Function, 
          lu.LookupValue} into g 
let Test_Count = ps.Count(x=>x.test_Id) 
let Test_Completed = ps.Sum(x=>x.completed) 
select new UserViewModel 
         { 
          User_ID = g.Key.User_ID, 
          Forename = g.Key.Forename, 
          Surname = g.Key.Surname, 
          Client_Code = g.Key.Client_Code, 
          User_Name = g.Key.User_Name, 
          Password = g.Key.Password, 
          Email = g.Key.Email, 
          Gender = g.Key.Gender, 
          Report_Date = g.Key.Report_date, 
          Email_Date = g.Key.EmailDate, 
          Test_Count = Test_Count, 
          Test_Completed = Test_Completed, 
          Job_Function = u.Job_Function, 
          Lookup_Value = lu.LookupValue 
         }); 
+0

這真的很有幫助,謝謝!我對分組的工作原理有了更好的理解。我仍然遇到Test_Count和Test_Completed的問題。我沒有提及它們是布爾值,我試圖將它們的總數歸還爲真。 – user1688784

+0

我沒有讓你回到真正的?你的意思是,如果count> 0是你想要返回的? – FosterZ

+0

對不起,沒有表格有點不清楚。沒有分組,結果返回將是多個具有相同信息但不同的Test_ID的UserViewModel條目。我試圖計算已經完成的測試次數。我對SQL查詢的理解是可以計算Completed的布爾值,從而給出完成的測試總數。對於Test_Count,這將是鏈接User_ID的測試的數量。希望這能說明問題。 – user1688784