简体   繁体   中英

Adding Grouped Count to Linq Query

I have the following fields (obviously a simplified version of the actual domain)

MyEntity.ID,
MyEntity.Title,
MyEntity.ParentID,
MyEntity.RootID,
MyEntity.IsOpen

If an entity that has no parent spawns a child the ParentID is set as the ID of the parent entity and the RootID is set as the same. If that child then spawns a child then its child's parentID is it's direct parent's ID, but the Root is the ID of the eldest descendent, so:

ID     Title     ParentID     RootID    IsOpen
1      One       NULL         NULL      True
2      Two       1            1         False
3      Three     2            1         True
4      Four      NULL         NULL      True

I want a query that will return the ID and Title of each entity, plus a Boolean that is True IF the entity is open AND there are any other Open entities that are related.

So For the above table Id expect to see

ID     Title    IsOpenAndHasHasOpenRelations
1      One      True
2      Two      False
3      Three    True
4      Four     False

The IsOpenAndHasHasOpenRelations column could equally just be a count of the number of open relations

The logic is basically - Assuming entity is open, does it have a parent? If not then are there any open entities that are its children? If it does have a parent are there any open entities that share to the same root?

I'm struggling to do this as a Linq to Entities Expression though. Can anyone help?

Edit. Here is the (slightly modified) current SQL that I'm attempting to move to LINQ:

WITH PageIndex AS (
SELECT *
FROM EntityTable) 
SELECT 
[PageIndex].*   
,CAST (CASE 
    WHEN EntityRecord.IsOpen = THEN 0
    WHEN EntityRecord.[RootID] IS NOT NULL THEN
        CASE
        WHEN ( SELECT COUNT(ID) FROM EntityTable AS rootEntity
            WHERE rootEntity.[ID] = PageIndex.[RootID]
            AND (rootEntity.IsOpen = 1)
            ) > 0 THEN 1
            ELSE 0
        END
    ELSE
        CASE
        WHEN ( SELECT COUNT(*) FROM EntityTable AS upissue
            WHERE upissue.RootID = PageIndex.[ID]
            AND (upissue.IsOpen = 1)
            ) > 0 THEN 1
            ELSE 0
        END
END AS BIT) AS IsMultipleLinkexEntitiesOpen
FROM PageIndex

The exact same logic in LinQ here:

var res = DBcontext.EntityTable.Select(x => new
                {
                    ID = x.ID,
                    Title = x.Title,
                    IsOpenAndHasHasOpenRelations = x.IsOpen ? false
                    : x.RootID != null ? DBcontext.EntityTable.Any(y => y.ID == x.RootID && y.IsOpen)
                    : (DBcontext.EntityTable.Any(y => y.ID == x.RootID && y.IsOpen))
                });

It could become cleanier if you hav foreign keys your entity to it self.

I still could not understand your full logic, i just translate what i see in SQL.

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