1

我正在尝试将以下不是我自己编写的 SQL 转换为 Linq to Entity 查询。

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的Groupand函数?Count()

4

1 回答 1

0

很好地围绕这个解决方案进行调整,因为目前我没有.Net 环境来测试这个解决方案,但你肯定会知道grouping在 linq 中是如何完成的。

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
                      });
于 2012-09-21T12:58:48.297 回答