用SQL查询替换EF LINQ查询组包含组
我们有一个EF LINQ查询,与以下相同的连接和组和组。
var subgroupquery1 = from sg in _dbContext.SubGroups
join e in _dbContext.Admins.Where(e => e.Status.StartsWith("Active") && e.SubGroupCode != indivsubgroupcode)
on sg.SubGroupCode equals e.SubGroupCode
join mf in _dbContext.MasterSetups.Where(e => groupstoinclude.Contains(e.AdminGroup)).Select(mfs => new { mfs.RefCode})
on e.EntCode equals mf.RefCode
group e by sg into g
select new SubGroupInfo
{
SubGroupKey = g.Key.SubGroupCode,
SubGroupCode = g.Key.SubGroupCode,
SubGroupName = string.Concat(g.Key.Name, " (", g.Key.SubGroupCode, ")"),
MasterGroupCode = g.Key.MasterGroupCode,
NoOfEntities = g.Count(),
Managers = g.Select(x => x.Manager).Distinct(),
Officers = g.Select(x => x.Officer).Distinct(),
ClientType = g.Key.ClientType,
Coordinator = g.Key.Coordinator,
GroupDate = g.Key.Group_Date
};
查询的表有大量列。 EF正在生成复杂的SQL查询,并不必要地选择所有列,使其缓慢而沉重。
我正在尝试将此LINQ查询转换为SQL查询,如下所示。但是我无法在分组后选择列。有人可以帮忙完成查询吗?
SELECT sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
FROM vw_W360_SubGroup sg
JOIN vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
JOIN vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
SELECT
{
-- how to select the columns to return values like LINQ query
}
We have an EF LINQ query with JOINs and GROUP BY and COUNT as below.
var subgroupquery1 = from sg in _dbContext.SubGroups
join e in _dbContext.Admins.Where(e => e.Status.StartsWith("Active") && e.SubGroupCode != indivsubgroupcode)
on sg.SubGroupCode equals e.SubGroupCode
join mf in _dbContext.MasterSetups.Where(e => groupstoinclude.Contains(e.AdminGroup)).Select(mfs => new { mfs.RefCode})
on e.EntCode equals mf.RefCode
group e by sg into g
select new SubGroupInfo
{
SubGroupKey = g.Key.SubGroupCode,
SubGroupCode = g.Key.SubGroupCode,
SubGroupName = string.Concat(g.Key.Name, " (", g.Key.SubGroupCode, ")"),
MasterGroupCode = g.Key.MasterGroupCode,
NoOfEntities = g.Count(),
Managers = g.Select(x => x.Manager).Distinct(),
Officers = g.Select(x => x.Officer).Distinct(),
ClientType = g.Key.ClientType,
Coordinator = g.Key.Coordinator,
GroupDate = g.Key.Group_Date
};
The tables being queried have large number of columns.
EF is generating a complex SQL query and unnecessarily selecting all columns making it slow and memory heavy.
I am trying to convert this LINQ query to SQL query, something like below. But I am not able to select the columns after grouping. Can someone help to complete the query please?
SELECT sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
FROM vw_W360_SubGroup sg
JOIN vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
JOIN vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
SELECT
{
-- how to select the columns to return values like LINQ query
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您进行选择查询时,您要选择和返回的所有列应仅在第一个选择语句中。
不同类型的SQL连接
以下是SQL中的连接的不同类型:(
左表
以及右表右表(外部)加入的匹配记录
从右表返回所有记录,并匹配的记录
从左表
是左或右表中的匹配项,
您可以选择更适合您的场景的联接类型。
参考:
https://www.w3schools.com/sql/sql/sql_join.asp
When you are doing a SELECT query, all the columns that you want to select and return should be only on the your first SELECT statement.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
tables
and the matched records from the right table
Returns all records from the right table, and the matched records
from the left table
is a match in either left or right table
You can select the type of join more suitable for your scenario.
References:
https://www.w3schools.com/sql/sql_join.asp