之后确定滤波器参数如何选择使用LINQ数据 [英] How to select data with linq after filter parameters are determined

查看:93
本文介绍了之后确定滤波器参数如何选择使用LINQ数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有其中用户可以选择不同的滤波器的报告应用于数据集

I have a report in which the user can select different filters to apply to a dataset.

在设计时,我不知道什么过滤用户将应用,所以我抓住了整个数据集。

At design time, I don't know what filters the user will apply, so I grab the whole dataset.

例如,如果用户希望看到,住在芝加哥的接触,我先抢到的所有联系人...

For instance, if the user wants to see contacts that live in Chicago, I first grab all contacts...

IList<contact> contacts = db.contacts.ToList();

然后我检查的形式收集过滤器和应用它们...

then I check the form collection for filters and apply them...

contacts = contacts.Where(t => t.state == form["state"]).ToList();

问题是,让所有接触到来自下渗是资源密集型的。有没有办法等待检索来自分贝接触,直到我有查询的所有参数?

The issue is that getting all contacts to filter down from is resource intensive. Is there a way to wait to retrieve the contacts from the db until I have all the parameters for the query?

我知道我这样做不对,但我需要知道解决这个正确的方法。

I realize I'm doing this wrong, but I need to know the correct way to approach this.

推荐答案

只是不要做第一次调用了ToList()。这所有的数据拉低到内存中。虽然你没有这样做,该数据是在的IEnumerable 的形式。这将使用懒惰评估,这意味着要枚举没有实际生产,直到他们要求的项目。在LINQ to SQL的情况下,数据库查询不会被运行,直到你做的第二个呼叫了ToList()

Just don't do the first call to ToList(). This pulls down all the data into memory. While you haven't done this, the data is in the form of an IEnumerable. This uses 'lazy evaluation' which means that the items to be enumerated aren't actually produced until they are requested. In the case of Linq To SQL, the database query won't be run until you do the second call to ToList().

这是的IEnumerable 是不是一个真正的容器,而这是一个对象,包含一张code,它知道如何获得下一个元素序列。这块$ C $的c可以是各种事物 - 它可以从实际的容器中读出,在即时产生的每个值,或者从其他地方获得的值,如数据库。清单然而,的容器。当你调用了ToList ,下一个项目是重复地从的IEnumerable提取并放置在名单。在这一点上,你必须在内存中的对象完全是一个集合。你经常打电话了ToList 当你想停下来有一个模糊的东西从得到的值的地方的并且有一个实际的容器充满真实元素。例如,的IEnumerable 可能不会给你同样的对象,每次使用它的时间。一旦你在让他们列表你知道,他们无法改变。在你的情况,你的希望的留在这个模糊的事,直到你决定你将要问的数据库是什么。

An IEnumerable is not really a container, rather it's an object, containing a piece of code that knows how to get the next element in a sequence. This piece of code could be various things - it could be reading from an actual container, generating each value on-the-fly, or getting the values from somewhere else, like a database. A list however, is a container. When you call ToList, the next item is repeatedly extracted from the IEnumerable and placed in a List. At this point you have exactly a collection of objects in memory. You often call ToList when you want to stop having a nebulous 'thing' which gets values from somewhere and have an actual container full of actual elements. For example, an IEnumerable might not give you the same objects each time you use it. Once you have them in a List you know that they can't change. In your case you want to stay with that nebulous thing until you have decided exactly what you are going to have to ask the database for.

这工作的方式是LINQ的根本,也是主要的原因是LINQ to SQL的是太好。我强烈建议您阅读一些有关LINQ的是如何工作的,也玩的名单和IEnumerable一点点。 C#传说乔恩斯基特的博客拥有的这个讨论负载,包括多大的LINQ的例子重新实现,所以你可以看到它可能工作。如果你读这一切你将是一个专家的LINQ(它也将是非常晚了),但是的这是一个介绍懒惰是如何工作的

The way this works is fundamental to LINQ, and also is the main reason that Linq To SQL is so nice. I highly recommend that you read something about how Linq works, and also play around with List and IEnumerable a little bit. C# legend Jon Skeet's blog has loads of discussion of this, including an example reimplementation of much of Linq so you can see how it might work. If you read all of it you will be a Linq expert (and it will also be very late), but this is an introduction to how laziness works.

这篇关于之后确定滤波器参数如何选择使用LINQ数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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