[英]SQL Query to Linq to Entities - C#
I have been trying to convert this SQL statement into a linq as i am trying to move the functionality into a program. 我一直在尝试将此SQL语句转换为linq,因为我正在尝试将功能转移到程序中。
Here is the SQL statement 这是SQL语句
SELECT cust.sg_group_name AS customer,
(SELECT Sum(du.used_space)
FROM sg_groups AS clnt
LEFT JOIN client_disk_usage AS du
ON clnt.sg_group_id = du.sg_group_id
AND clnt.group_role_id = 3
WHERE clnt.parent_group_id = cust.sg_group_id
AND du.day_of_month = 15
AND du.month_of_year = 05
AND du.used_space_year = 2016) AS disk_usage
FROM sg_groups AS cust
WHERE cust.group_role_id = 2
ORDER BY cust.sg_group_name
Essentially the output is just a list with two columns 本质上,输出只是两列的列表
customer disk_usage
Customer1 136401537652
Customer2 42208008210
If possible i just want to convert this to a linq statement. 如果可能的话,我只想将其转换为linq语句。 I have tried putting the query into LinqPad, but it doesn't seem to want to convert from SQL to Linq (just comes up with a blank white page). 我曾尝试将查询放入LinqPad,但它似乎不想从SQL转换为Linq(只是出现了一个空白的白页)。 I have had a crack at the query myself, but i either get something that doesn't work altogether, or an incorrect number of results. 我自己在查询方面遇到了麻烦,但是我得到的东西根本无法正常工作,或者结果数量不正确。
If anyone has any suggestions that would be great! 如果有人有任何建议,那就太好了!
disk_usage(Sub Query) is a bit Complicated Part. disk_usage(子查询)是一个有点复杂的部分。 Converted over here. 在这里转换。 Try this out 试试看
var CoreList = (from clnt in EntityName.sg_groups
join du in EntityName.client_disk_usage
on new { GrpId = clnt.sg_group_id, RoleId = clnt.group_role_id } equals new { GrpId = du.sg_group_id, RoleId = 3 } into LJ
from RT in LJ.DefaultIfEmpty()
where du.day_of_month == 15 && du.month_of_year == 05 && du.used_space_year == 2016
select new {clnt, du, RT}
).ToList();
var CoreListSet = CoreList.Select(i=> new YourEntityClass
{
//Fetch the ParentGroupId & UsedSpace
}).ToList();
var CoreListComplete = (from cl in CoreListSet
join cust in EntityName.sg_groups
on cust.sg_group_id equals cl.parent_group_id).ToList();
Now get the sum of CoreListComplete & just implement the base Select Query in Linq! 现在获取CoreListComplete的总和,并在Linq中实现基本的Select Query!
Apologies for the delayed response. 对于延迟的回复表示歉意。 I've marked @Anil answer up as this is the one that helped me find the answer. 我已将@Anil答案标记为,因为这是帮助我找到答案的答案。 You solution did work @Sathish but it can be accomplished in a single command. 您的解决方案确实可以使用@Sathish,但可以通过单个命令来完成。 Here is my final solution. 这是我的最终解决方案。 Many thanks for your help! 非常感谢您的帮助!
storeGridUsage = (
from cust in db.sg_groups
from client in db.sg_groups
join du in db.client_disk_usage on client.SG_GROUP_ID equals du.SG_GROUP_ID
where client.GROUP_ROLE_ID == 3
where client.PARENT_GROUP_ID == cust.SG_GROUP_ID && du.DAY_OF_MONTH == day && du.MONTH_OF_YEAR == month && du.USED_SPACE_YEAR == year
where cust.GROUP_ROLE_ID == 2
orderby cust.SG_GROUP_NAME
group new {cust, du} by cust.SG_GROUP_NAME
into g
select new StoreGridUsage
{
CustomerName = g.Key,
DiskUsageInBytes = g.Sum(o => o.du.USED_SPACE)
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.