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 の機能を複製するGroupにはどうすればよいですか?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 に答える