如何在.Take()之前获取总结果计数-但是在使用.Take()时 [英] How to get total results count before .Take() - but when using .Take()

查看:102
本文介绍了如何在.Take()之前获取总结果计数-但是在使用.Take()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Take()获得固定数量的结果.

I am using .Take() to get a fixed number of results.

获得TotalCountBeforeTake的最佳方法是什么(即好像我没有使用.Take()一样)?

What is the best way to get the TotalCountBeforeTake (ie as if I didn't use the .Take())?

我能否在不运行两次查询的情况下获得TotalCountBeforeTake?

Can I get the TotalCountBeforeTake without running the query twice?

        var Results = (from results in db.FindWords(term)
                       orderby results.word
                       select results.word).Take(100);

        //just to get the total record count
        int TotalCountBeforeTake = (from results in db.FindWords(term)
                            select results.word).Count();

        // only showing 100 out of TotalCountBeforeTake results,
        // but in order to know the TotalCountBeforeTake I had to run the query twice.
        foreach (var result in Results)
        {
            Console.Write(result.ToString());
        }

推荐答案

您要查询两件事-项目总数和项目子集.因此,您需要运行两个查询:

You want to query two things - the total number of items and a subset of items. So you need to run two queries:

    // Define queries
    var query1 = from results in db.FindWords(term)
                 orderby results.word
                 select results.word;

    var query2 = query1.Take(100);

    // Run queries
    int totalCountBeforeTake = query1.Count();

    foreach (var result in query2)
    {
        Console.Write(result.ToString());
    }

这篇关于如何在.Take()之前获取总结果计数-但是在使用.Take()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆