使用 Linq To SQL 返回未读记录

发布于 2024-08-10 11:42:35 字数 1945 浏览 3 评论 0原文

我不知道如何问这个问题,所以我将从我正在做的事情的例子开始。

这是我的表结构...

文档(主键 = ID)
ID、标题、LatestApprovedRevID

修订(主键 = ID)
ID、DocumentID、RevisionNum、正文

Document_Reads(主键 = DocumentID、用户名)
DocumentID、UserName、RevisionID

当用户打开文档时,将打开最新批准的修订版本,并将一条记录插入到 Document_Reads 中,显示用户已阅读的文档和修订版本。我想知道如何使用 Linq 查询以返回 UserName(当前经过身份验证的用户)尚未读取的文档。

要获取未读文档的列表,我想在三种情况下返回文档:

  • 如果文档在 Document_Reads 中没有记录。
  • 如果文档在 Document_Reads 中有记录,但没有一条记录是针对 UserName 的。
  • 如果文档在 Document_Reads 中有记录并且存在 UserName 记录,但 Document_Reads.RevisionID 与 Documents.LastApprovedRevID 不匹配。

我已经编写了一个过滤函数,但在编写第二个要求的查询时遇到问题。如果发现任何 Document_Reads 与用户名不匹配,它将返回文档。

<Runtime.CompilerServices.Extension()> _
Public Function FilterLatestUnread(ByVal query As IQueryable(Of Document), ByVal userName As String) As IQueryable(Of Document)

    'Documents with no document_reads
    Dim q As IQueryable(Of Document) = From d In query _
                                       Where d.Document_Reads.Count = 0 _
                                       Select d

    'documents with document_reads but none for userName
    q = q.Union(From d In query _
                From dr In d.Document_Reads _
                Where Not (dr.UserName = userName) _
                Select d)

    'documents with document_reads for userName but RevisionID does not match LastApprovedRevID
    q = q.Union(From d In query _
                From dr In d.Document_Reads _
                Where dr.UserName = userName And _
                Not (dr.RevisionID = d.LastApprovedRevID) _
                Select d)

    'Return the combined query.
    Return q

End Function

然后,我让我的存储库返回所有已批准修订的文档,并使用过滤器,例如...

return _repository.List().FilterLatestUnread("John Doe").ToList()

感谢您的帮助,如果已经可以在此站点上找到此主题,我深表歉意...我不确定我需要什么去寻找。

I'm not sure how to ask this question, so I'll start with an example of what I'm doing.

Here is my table structure...

Documents (Primary key = ID)
ID, Title, LatestApprovedRevID

Revisions (Primary key = ID)
ID, DocumentID, RevisionNum, Body

Document_Reads (Primary key = DocumentID, UserName)
DocumentID, UserName, RevisionID

When a user opens a document, the latest approved revision is opened and a record is inserted into Document_Reads showing the document and revision the user had read. I would like to know how to query using Linq to return the documents that have NOT been read by UserName (the current authenticated user).

To get the list of unread documents, there are three cases where I would like to return the document:

  • if a document has no records in Document_Reads.
  • if a document has records in Document_Reads, but none of them are for UserName.
  • if a document has records in Document_Reads and one for UserName exists, but the Document_Reads.RevisionID does not match Documents.LastApprovedRevID.

I have written a filter function, but I'm having problems writing the query for the 2nd requirement. It will return the document if any Document_Reads are found that do not match the UserName.

<Runtime.CompilerServices.Extension()> _
Public Function FilterLatestUnread(ByVal query As IQueryable(Of Document), ByVal userName As String) As IQueryable(Of Document)

    'Documents with no document_reads
    Dim q As IQueryable(Of Document) = From d In query _
                                       Where d.Document_Reads.Count = 0 _
                                       Select d

    'documents with document_reads but none for userName
    q = q.Union(From d In query _
                From dr In d.Document_Reads _
                Where Not (dr.UserName = userName) _
                Select d)

    'documents with document_reads for userName but RevisionID does not match LastApprovedRevID
    q = q.Union(From d In query _
                From dr In d.Document_Reads _
                Where dr.UserName = userName And _
                Not (dr.RevisionID = d.LastApprovedRevID) _
                Select d)

    'Return the combined query.
    Return q

End Function

Then, I have my repository return all documents that have an approved revision and use the filter like...

return _repository.List().FilterLatestUnread("John Doe").ToList()

Thanks for any help and I apologize if this topic can be found on this site already...I wasn't sure what I needed to search for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-08-17 11:42:35

我想我明白了。

对于第二个查询,我使用以下代码:

 q = q.Union(From d In query _
                Where (From dr In d.Document_Reads _
                       Where dr.UserName = userName _
                       Select dr).Count = 0 _
                       Select d)

感谢 bpayne 帮助我解决这个问题!

I think I figured it out.

For the second query, I'm using this code:

 q = q.Union(From d In query _
                Where (From dr In d.Document_Reads _
                       Where dr.UserName = userName _
                       Select dr).Count = 0 _
                       Select d)

Thanks bpayne for helping me talk it out!

说谎友 2024-08-17 11:42:35

更新:
我遇到了另一个错误,该错误与尝试使用不同数量的表达式进行联合查询有关。这就是我最终必须做的来解决它......

return From d In query _
       Where (d.Document_Reads.Count = 0) Or _
       (From dr In d.Document_Reads _
        Where dr.UserName = userName _
        Select dr).Count = 0 Or _
        (From dr In d.Document_Reads _
         Where dr.UserName = userName And _
         Not (dr.RevisionID = d.LastApprovedRevID) _
         Select dr).Count = 1 _
         Select d

Update:
I had run into another error that had to do with trying to Union queries with a different number of expressions. This is what I had to do to fix it in the end...

return From d In query _
       Where (d.Document_Reads.Count = 0) Or _
       (From dr In d.Document_Reads _
        Where dr.UserName = userName _
        Select dr).Count = 0 Or _
        (From dr In d.Document_Reads _
         Where dr.UserName = userName And _
         Not (dr.RevisionID = d.LastApprovedRevID) _
         Select dr).Count = 1 _
         Select d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文