【问题标题】:Converting SQL to LINQ to Entity将 SQL 转换为 LINQ 到实体
【发布时间】:2012-09-13 21:02:32
【问题描述】:

我正在尝试将以下不是我自己编写的 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的GroupCount()函数?

【问题讨论】:

  • 如果它需要你永远去做。那为什么呢?

标签: sql linq count linq-to-entities linq-group


【解决方案1】:

围绕这个解决方案进行调整,因为目前我没有 .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
                      });

【讨论】:

  • 这真的很有帮助,谢谢!我对分组的工作原理有了更好的理解。我仍然遇到 Test_Count 和 Test_Completed 的问题。我没有提到它们是布尔值,我试图将它们的总数返回为真。
  • 我没有告诉你返回 true 吗?你的意思是如果count > 0是你想要返回的??
  • 抱歉,没有表格有点不清楚。如果没有分组,结果返回将是多个具有相同信息但不同 Test_ID 的 UserViewModel 条目。我正在尝试计算已完成的测试数量。我对 SQL 查询的理解是,可以计算 Completed 的布尔值,从而给出已完成测试的总数。对于 Test_Count,这将是链接到 User_ID 的测试数。希望能解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 2013-05-19
相关资源
最近更新 更多