分组数据并使用 Entity Framework 检索分组的每一行
Group data and retrieve every line of the grouping with Entity Framework
我在想,一旦在 C# 部分中检索到分组数据,我就可以循环遍历分组的项目列表。
var res = db.Commandes.Where(t => t.idMatiere == mod.idMatiereChoisie).GroupBy(t => t.UA_idCa);
foreach(var group in res)
{
foreach(var groupedLines in group)
{
// Always a single line, this loop is useless
}
}
这里应用的逻辑似乎比 C# 更像 SQL:分组结果在一行中,您不会看到所有分组的项目。
不是我克服不了的问题
我将使用的策略:不分组,我只查询所有行,然后在循环时验证UA_idCa
是否不同形成以前的数据,这意味着已经达到下一个 "group"。
但我想知道...如果可能的话,人们通常如何干净地做到这一点?
是否需要重新查询才能检索群组的内容?
或者“我将使用的策略”是否更接近于最佳策略?
这个问题是 SQL 服务器 AND Entity Framework.
组合的问题
似乎必须将分组部分中的值之一(组内所有行的值都不同)标记为非空。
因为在查找可能是键的内容时,实体不会在乎可为空的值:它们可能是唯一的,它们可能永远不会为空,EF 甚至不会检查这一点。
一旦在 sql 部分被标记为 NOT NULL,EF 突然明白分组部分可能有多个不同的唯一值...
所以基本上是这样的:
ALTER view [dbo].[Commandes] as
SELECT top(50000000)
isnull(ex.unitAdm, '000') UnitAdm
,c.id as idCahier
,isnull(ex.unitAdm, '000') + cast(c.id as nvarchar(6)) as UA_idCa
,c.NomCahier
,[Qte]
,c.prix as PrixCahier
,sc.id, 0 as idSousCahier /* THIS IS WHAT I COULD NOT COMPLETELY RETRIEVE
because it could be null ? */
,sc.NomCahier as sousCahier
,sc.prix as PrixSC
,m.id as idMatiere
,m.Code
,m.NomMatiere
,ep.id as idEpreuve
,ep.Titre
FROM [CahierExamen] cex
join Cahier c on c.id = cex.Fk_Cahier
join Examen ex on cex.FK_Examen = ex.id
join epreuve ep on ex.FK_Epreuve = ep.id
join Matiere m on ep.FK_Matiere = m.id
left join SousCahier sc on c.id = sc.FK_Cahier
order by code, unitAdm, idCahier
GO
已更改为:
ALTER view [dbo].[Commandes] as
SELECT top(50000000)
isnull(ex.unitAdm, '000') UnitAdm
,c.id as idCahier
,isnull(ex.unitAdm, '000') + cast(c.id as nvarchar(6)) as UA_idCa
,c.NomCahier
,[Qte]
,c.prix as PrixCahier
,isnull(sc.id, 0) as idSousCahier /* WOW, NOW EF UNDERSTAND
THERE COULD BE MULTIPLE DIFFERENTS VALUES ONCE DATA ARE GROUPED*/
,sc.NomCahier as sousCahier
,sc.prix as PrixSC
,m.id as idMatiere
,m.Code
,m.NomMatiere
,ep.id as idEpreuve
,ep.Titre
FROM [CahierExamen] cex
join Cahier c on c.id = cex.Fk_Cahier
join Examen ex on cex.FK_Examen = ex.id
join epreuve ep on ex.FK_Epreuve = ep.id
join Matiere m on ep.FK_Matiere = m.id
left join SousCahier sc on c.id = sc.FK_Cahier
order by code, unitAdm, idCahier
GO
我在想,一旦在 C# 部分中检索到分组数据,我就可以循环遍历分组的项目列表。
var res = db.Commandes.Where(t => t.idMatiere == mod.idMatiereChoisie).GroupBy(t => t.UA_idCa);
foreach(var group in res)
{
foreach(var groupedLines in group)
{
// Always a single line, this loop is useless
}
}
这里应用的逻辑似乎比 C# 更像 SQL:分组结果在一行中,您不会看到所有分组的项目。
不是我克服不了的问题
我将使用的策略:不分组,我只查询所有行,然后在循环时验证UA_idCa
是否不同形成以前的数据,这意味着已经达到下一个 "group"。
但我想知道...如果可能的话,人们通常如何干净地做到这一点?
是否需要重新查询才能检索群组的内容?
或者“我将使用的策略”是否更接近于最佳策略?
这个问题是 SQL 服务器 AND Entity Framework.
组合的问题似乎必须将分组部分中的值之一(组内所有行的值都不同)标记为非空。
因为在查找可能是键的内容时,实体不会在乎可为空的值:它们可能是唯一的,它们可能永远不会为空,EF 甚至不会检查这一点。
一旦在 sql 部分被标记为 NOT NULL,EF 突然明白分组部分可能有多个不同的唯一值...
所以基本上是这样的:
ALTER view [dbo].[Commandes] as
SELECT top(50000000)
isnull(ex.unitAdm, '000') UnitAdm
,c.id as idCahier
,isnull(ex.unitAdm, '000') + cast(c.id as nvarchar(6)) as UA_idCa
,c.NomCahier
,[Qte]
,c.prix as PrixCahier
,sc.id, 0 as idSousCahier /* THIS IS WHAT I COULD NOT COMPLETELY RETRIEVE
because it could be null ? */
,sc.NomCahier as sousCahier
,sc.prix as PrixSC
,m.id as idMatiere
,m.Code
,m.NomMatiere
,ep.id as idEpreuve
,ep.Titre
FROM [CahierExamen] cex
join Cahier c on c.id = cex.Fk_Cahier
join Examen ex on cex.FK_Examen = ex.id
join epreuve ep on ex.FK_Epreuve = ep.id
join Matiere m on ep.FK_Matiere = m.id
left join SousCahier sc on c.id = sc.FK_Cahier
order by code, unitAdm, idCahier
GO
已更改为:
ALTER view [dbo].[Commandes] as
SELECT top(50000000)
isnull(ex.unitAdm, '000') UnitAdm
,c.id as idCahier
,isnull(ex.unitAdm, '000') + cast(c.id as nvarchar(6)) as UA_idCa
,c.NomCahier
,[Qte]
,c.prix as PrixCahier
,isnull(sc.id, 0) as idSousCahier /* WOW, NOW EF UNDERSTAND
THERE COULD BE MULTIPLE DIFFERENTS VALUES ONCE DATA ARE GROUPED*/
,sc.NomCahier as sousCahier
,sc.prix as PrixSC
,m.id as idMatiere
,m.Code
,m.NomMatiere
,ep.id as idEpreuve
,ep.Titre
FROM [CahierExamen] cex
join Cahier c on c.id = cex.Fk_Cahier
join Examen ex on cex.FK_Examen = ex.id
join epreuve ep on ex.FK_Epreuve = ep.id
join Matiere m on ep.FK_Matiere = m.id
left join SousCahier sc on c.id = sc.FK_Cahier
order by code, unitAdm, idCahier
GO