简体   繁体   English

如何异步获取和选择真正正确的最新数据

[英]How to get and select real correct Data Asynchronously up to date

In a website, there are an article page and when i go to the article page, it displays the article and the first 10 comments belong to this article. 在网站上,有一个文章页面,当我转到该文章页面时,它将显示该文章,而前10条评论属于该文章。

And there are some button when click it, it asynchronously display the rest of all comments by AJAX. 并且当单击它时有一些按钮,它通过AJAX异步显示所有注释的其余部分。

The question is that suppose after the article page is loaded with first 10 comments, User that own comment or Admin delete it, how to select the rest of comments? 问题是,假设在文章页面加载了前10条评论之后,拥有评论或Admin的用户将其删除,如何选择其余评论? if i skip first 10 comments from select statement, there are some comments not selected. 如果我从选择语句中跳过前10条评论,则有一些未选择的评论。

As Example: 例如:

suppose the comments IDs are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 假设评论ID为:1、2、3、4、5、6、7、8、9、10、11、12、13、14、15、16、17、18、19、20

First 10 comments IDs i select are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 我选择的前10条评论ID为:1、2、3、4、5、6、7、8、9、10

IEnumerable<Comment> comments = (from a in context.Comments
                                         where a.ArticleID == ArticleID
                                         orderby a.CommentDate descending
                                         select new Comment
                                         {
                                             CommentID = a.CommentID,
                                             CommentContent = a.CommentContent,
                                             CommentDate = a.CommentDate,


                                         }).ToList().Take(10);

suppose User that own comment or Admin delete 2 comments whose IDs: 9, 10 假设拥有评论或管理员的用户删除2条ID:9、10的评论

then the first ten comments in databse will become: 1, 2, 3, 4, 5, 6, 7, 8, 11, 12 那么数据库中的前十个注释将变为:1、2、3、4、5、6、7、8、11、12

when i try to get the rest of comments and skip first 10 comments, then the result is: 13, 14, 15, 16, 17, 18, 19, 20 当我尝试获取其余评论并跳过前10条评论时,结果是:13,14,15,16,17,17,18,19,20

IEnumerable<Comment> comments = (from a in context.Comments
                                         where a.ArticleID == ArticleID
                                         orderby a.CommentDate descending
                                         select new Comment
                                         {
                                             CommentID = a.CommentID,
                                             CommentContent = a.CommentContent,
                                             CommentDate = a.CommentDate,


                                         }).ToList().Skip(10);

so there are two comments 11 & 12 i cannot see them, because comments with IDs: 11, 12 will be consider with the first 10 comments... 所以我看不到两条注释11和12,因为ID为11、12的注释将与前10条注释一起考虑...

You need to use Skip and Take together in the same LINQ query, and the values passed to those functions should be dynamic based on what the data that was already presented in the application. 您需要在同一LINQ查询中一起使用“跳过”和“ 合并 ”,并且基于应用程序中已提供的数据,传递给这些函数的值应该是动态的。

So, for the initial page display, you would have a query like this: 因此,对于初始页面显示,您将有如下查询:

var toSkip = 0; var toTake = 10;

IEnumerable<Comment> comments = (from a in context.Comments
    where a.ArticleID == ArticleID
    orderby a.CommentDate descending
    select new Comment
    {
        CommentID = a.CommentID,
        CommentContent = a.CommentContent,
        CommentDate = a.CommentDate
    })
    .Skip(toSkip).Take(toTake).ToList();

Subsequent queries would change the values in the toSkip and toTake variables appropriately. 随后的查询将适当地更改toSkiptoTake变量中的值。 Ideally , these would be parameters sent into the MVC action that is used to get the data the application needs. 理想情况下 ,这些将是发送到MVC操作中的参数,该参数用于获取应用程序所需的数据。

With this approach, you will need to have a way to tell how many to skip based on the data left from the prior, or initial, list. 使用这种方法,您将需要有一种方法可以根据先前列表或初始列表中剩余的数据来判断要跳过的数量。 A LINQ query to get the count of items based on the Min and Max comment Id from the current list should allow you to derive the number to skip. 一个LINQ查询来基于当前列表中的Min和Max注释ID获取项目数,应该允许您得出要跳过的数字。 This query would take into consideration the data that was delete between requests, too. 该查询还将考虑在两次请求之间删除的数据。 It's a seemingly extra query, so I am not that excited about that. 这似乎是一个额外的查询,因此我对此并不感到兴奋。

You could use the MAX comment Id from the prior list, and filter like this. 您可以使用先前列表中的MAX注释ID,并像这样进行过滤。

var toTake = 10; var maxCommentId = 10;

IEnumerable<Comment> comments = (from a in context.Comments
    where a.ArticleID == ArticleID
    && a.CommentID > maxCommentId // <- new filter here
    orderby a.CommentDate descending
    select new Comment
    {
        CommentID = a.CommentID,
        CommentContent = a.CommentContent,
        CommentDate = a.CommentDate
    })
    .Take(toTake).ToList();

Again, the variable values would need to change based on the current application state; 同样,变量值将需要根据当前应用程序状态进行更改。 making them action parameters being ideal. 使它们的动作参数非常理想。 Something like this: 像这样:

public async IActionResult GetComments(int articleID,
  int toSkip = 0,
  int toTake = 10)
    {
        IEnumerable<Comment> comments = (from a in context.Comments
          where a.ArticleID == articleID
          orderby a.CommentDate descending
          select new Comment
          {
              CommentID = a.CommentID,
              CommentContent = a.CommentContent,
              CommentDate = a.CommentDate
          })
          .Skip(toSkip).Take(toTake).ToList();
        return comments;
    }

This is not a full solution, but it should give you an idea of what direction to take that works best for your application. 这不是一个完整的解决方案,但是它应该使您了解哪种方向最适合您的应用程序。 I hope it helps you out. 希望对您有所帮助。

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

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