简体   繁体   中英

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.

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.

It's not a problem that I can't overcome

: 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 " " closer to what's best?”更接近最佳策略?

This problem is a matter of the combination of SQL server Entity Framework. 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.

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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