LINQ性能 [英] LINQ performance

查看:105
本文介绍了LINQ性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读从数据库中的记录,并检查了一些条件,并存储在名单,其中;结果> 。结果是一类。然后执行LINQ查询名单,其中;结果> 像分组,计算等,所以有可能是机会,分50,000条记录的名单,其中;结果> ,所以这是否其更好地去LINQ(或)重新插入记录到数据库,并进行查询?

I am reading records from database and check some conditions and store in List<Result>. Result is a class. Then performing LINQ query in List<Result> like grouping, counting etc. So there may be chance that min 50,000 records in List<Result>, so in this whether its better to go for LINQ (or) reinsert the records to db and perform the queries?

推荐答案

为什么不将它存储在IQueryable的,而不是一个列表,并使用LINQ to SQL或LINQ到实体,实际的数据集将不会被拉高到内存中,并查询将真正深入到运行数据库。

Why not store it in an IQueryable instead of a List and using LINQ to SQL or LINQ to Entities, the actual dataset will never be pulled into memory, and the queries will actually go down to the database to run.

示例:

Database db = new Database(); // this is what L2E gives you...

var children = db.Person.Where(p => p.Age < 21); // no actual database query performed


// will do : "select count(*) from Person where Age < 21"
int numChildren = children.Count();

var grouped = children.GroupBy(p => p.Age); // no actual query

int youngest = children.Min(p => p.Age); // performs query

int numYoungest = youngest.Count(p => p.Age == youngest); // performs query.

var youngestNames = children.Where(p => p.Age == youngest).Select(p => p.Name); // no query

var anArray = youngestNames.ToArray(); // performs query

string names = string.join(", ", anArray); // no query of course

这篇关于LINQ性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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