如何构造包含子项的计数查询

时间:2014-03-06 16:55:22

标签: c# sql entity-framework hierarchy

我有一个消息类型层次结构:Request,可以包含Offer个,可以包含Dialog个。我正在尝试查询数据库,以确定每个链中有多少包含给定用户尚未读取的消息。

这是SQL中的工作查询(但如果可能的话,我希望能够在LINQ to Entities查询中执行此操作):

SELECT      COUNT(msgID)
FROM        tblMessage
WHERE       msgTypeID = 1 // Request type
    AND     msgUserID = 7 // The creator of the Request
    AND     (       msgWhenRead IS NULL // That they didn't read the top Request, OR has any Offers, etc that haven't been read
                OR  0 < (   SELECT  COUNT(msgID)
                            FROM    tblMessage
                            WHERE   msgTypeID = 2
                            AND     msgRecipientID = 7 // Looking for Recipient here, as the creator is the person who is making the offer, but the Recipient is being set to the person who made the parent Request.
                            AND     (       msgWhenRead IS NULL
                                        OR  0 < (   SELECT  COUNT(msgID)
                                                    FROM    tblMessage
                                                    WHERE   msgTypeID = 3 // Is a Dialog
                                                    AND     msgRecipientID = 7 // Same deal with the Recipient here as with Offers.
                                                    AND     msgWhenRead IS NULL
                                                )
                                    )
                        )
            )

这是我尝试使用EntityFramework:

response.Requests = repository.Messages
                              .OfType<Request>()
                              .Where(r => r.Creator.Id == request.UserId &&
                                          r.dbIsDeleted == "N" &&                                                                           
                                          r.dbStatusId != (int)RequestStatus.Inappropriate &&
                                          (     r.WhenRead == null ||
                                                r.Offers.Select(o => o.RecipientId == request.UserId && 
                                                                     o.dbIsDeleted == "N" &&
                                                                     o.dbStatusId != (int)OfferStatus.Inappropriate &&
                                                                     ( o.WhenRead == null ||
                                                                       o.Dialogs.Select(d => d.RecipientId == request.UserId &&
                                                                                             d.dbIsDeleted == "N" &&
                                                                                             d.dbStatusId != (int)DialogStatus.Inappropriate &&
                                                                                             d.WhenRead == null)
                                                                                .Any()
                                                                    ))
                                                        .Any()
                               ))
                               .Count();

我尝试在SelectManyr.Offers.Select()更改为o.Dialogs.Select(),但收到编译错误。我想也许这就是问题所在。

我现在得到的是,在阅读完所有内容后,实体框架查询仍在查找最高请求,我不知道为什么。生成的EF SQL的结构与我自己编写的结构并不相近。如果人们认为可以提供帮助,我可以提供EF SQL。

0 个答案:

没有答案