简体   繁体   English

使用实体框架对数据进行分组并检索分组的每一行

[英]Group data and retrieve every line of the grouping with Entity Framework

I was thinking that maybe, once the grouped data are retrieved in the C# part, I would be able loop through the list of items that were grouped.我在想,也许一旦在 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
    }
}

It seems the logic applied here is more like SQL than C#: the grouping result in a single line and you won't see all the grouped items.似乎这里应用的逻辑更像是 SQL 而不是 C#:分组结果在一行中,你不会看到所有分组的项目。

It's not a problem that I can't overcome不是我克服不了的问题

Tactic I will use : instead of grouping, I'll just query all the lines, and then, while looping, I will verify if UA_idCa is different form the previous data and that will means the next "group" has been reached.我将使用的策略是:我将只查询所有行,而不是分组,然后在循环时,我将验证UA_idCa是否与之前的数据不同,这将意味着已到达下一个“组”。

But I wonder... How does someone normally do this cleanly, if it's possible?但我想知道......如果可能的话,人们通常如何干净利落地做到这一点?

Do you have to query again to retrieve a group's content?您是否必须再次查询才能检索组的内容?

Or is the " Tactic I will use " closer to what's best?还是“我将使用的策略”更接近最佳策略?

This problem is a matter of the combination of SQL server AND Entity Framework.这个问题是SQL server AND Entity Framework的组合问题。

Seems like one of the value in the grouped part (a value that is different for all the line inside the group) must be marked as not null.似乎分组部分中的值之一(组内所有行的值都不同)必须标记为非空。

Because when looking for what could be a key, entity doesn't give a damn about nullable values : they could be unique, they could be never null, EF won't even check that.因为在寻找可能是键的内容时,实体并不关心可空值:它们可能是唯一的,也可能永远不会为空,EF 甚至不会检查它。

Once it is marked as NOT NULL in the sql part, EF suddenly understand that there could multiple different unique values in the grouped part...一旦在sql部分被标记为NOT NULL,EF突然明白分组部分可能有多个不同的唯一值......

So basically This :所以基本上这个:

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

As been changed to this:改成这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM