Iqueryable.where.tolist VS iqueryable.tolist.where,哪个查询在性能方面更好? [英] Iqueryable.where.tolist VS iqueryable.tolist.where, which query is better in terms of performance?

查看:103
本文介绍了Iqueryable.where.tolist VS iqueryable.tolist.where,哪个查询在性能方面更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题,两个都产生相同的结果,但我想知道哪一个更有效。以下是查询,我只编写了查询子句。两个查询的内部条件相同。



IQueryable()。Where()。ToList();

IQueryable()。 ToList()。Where();



在我的最后,我尝试了下面的代码并显示IQueryable()。ToList()。Where();更好。我不明白的问题很少:1。没有看到我的下面的临时代码哪个查询更有效? 2.据我所知,IQueryable有助于查询远程数据。那么,不应该首先过滤掉项目然后使用ToList以便我们不需要在非相关项目上执行ToList功能? (如果是这种情况那么为什么下面的代码说查询2更有效?)



I'm having two queries, both are producing same result but I wanna know which one is more efficient. Below are the queries, I've written the query clauses only. The inner conditions are same for both the queries.

IQueryable().Where().ToList();
IQueryable().ToList().Where();

At my end I've tried below code and it's showing "IQueryable().ToList().Where();" is better. There are few questions that I don't understand: 1. Without seeing my below temporary code what which query is more efficient? 2. As fas as I know, IQueryable is good for querying remote data. So, should not be it better first to filter out the items and then use ToList so that we don't need to perform ToList function on non relevant items? (if this is the case then why below code is saying query 2 is more efficient?)

Stopwatch st1 = new Stopwatch();
            Stopwatch st2 = new Stopwatch();
            int counter = 10000;
            IEnumerable<Employee> iEmp = null;
            IQueryable<Employee> qEmp = null;
            BindingList<Employee> bList = new BindingList<Employee>();
            for (int i = 1; i <= counter; ++i)
            {
                bList.Add(new Employee
                {
                    Department = $"Dept - {i}",
                    EmployeeID = i,
                    EmployeeName = $"Employee - {i}",
                    Salary = i + 10000
                });
            }

            iEmp = bList.AsEnumerable<Employee>();
            qEmp = bList.AsQueryable<Employee>();

            st1.Start();
            var t = qEmp.Where(x => x.EmployeeID % 2 == 0).ToList();
            st1.Stop();
            Console.WriteLine($"Queryable-Where-ToList: {st1.ElapsedTicks}");

            st2.Start();
            var t1 = qEmp.ToList().Where(x => x.EmployeeID % 2 == 0);
            st2.Stop();
            Console.WriteLine($"Queryable-ToList-Where: {st2.ElapsedTicks}");
            Console.ReadKey();





我尝试过:



我试过一个例子,但我不满意,因为它与我的理论相矛盾,我无法证明结果。



What I have tried:

I tried an example but I am not satisfied because it contradict with my theory and I am not able to justify the result.

推荐答案

Dept - {i}
EmployeeID = i,
EmployeeName =
"Dept - {i}", EmployeeID = i, EmployeeName =


员工 - {i}
薪水= i + 10000
});
}

iEmp = bList.AsEnumerable< Employee>();
qEmp = bList.AsQueryable< Employee>();

st1.Start();
var t = qEmp.Where(x = > x.EmployeeID% 2 == 0 )。ToList();
st1.Stop();
Console.WriteLine(
"Employee - {i}", Salary = i + 10000 }); } iEmp = bList.AsEnumerable<Employee>(); qEmp = bList.AsQueryable<Employee>(); st1.Start(); var t = qEmp.Where(x => x.EmployeeID % 2 == 0).ToList(); st1.Stop(); Console.WriteLine(


Queryable-Where-ToList: {st1.ElapsedTicks});

st2.Start();
var t1 = qEmp.ToList()。其中​​(x = > x.EmployeeID % 2 == 0 );
st2.Stop();
Console.WriteLine(
"Queryable-Where-ToList: {st1.ElapsedTicks}"); st2.Start(); var t1 = qEmp.ToList().Where(x => x.EmployeeID % 2 == 0); st2.Stop(); Console.WriteLine(


这篇关于Iqueryable.where.tolist VS iqueryable.tolist.where,哪个查询在性能方面更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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