tags:

views:

94

answers:

3

Say I have simple LINQ extenstion:

var filtered = data.Where(i => i.Count > 0);

I know that this will not evaluated unless I start to use filtered i.e.

foreach(DataItem i in filtered) ...

However I need to create cloned version of data and release data as soon as possible. data can be changed in other thread so I want to get immediate snapshot of it.

How can I force filtered to be evaluated immediately?

+6  A: 

To get a list populated with the result of the enumeration, call ToList(), i.e.,

var filtered = data.Where(i => i.Count > 0).ToList(); 
jball
+4  A: 
var filtered = data.Where(i => i.Count > 0).ToArray();

// or

var filtered = data.Where(i => i.Count > 0).ToList();
LukeH
+1  A: 

To side-effect the sequence:

var filtered = data.Where(i => i.Count > 0).Run();

You may want to explore Reactive Extension for .NET 3.5/4.0.

Kthurein