IEnumerable Multiple Enumeration

I know that IEnumerable has been discussed several times here, but I could not find the answer to my specific question, so I raise it as a new question.

Consider the following code snippet:

static void Main(string[] args) { List<string> testList = new List<string> {"Test", "Test1", "Test1"}; IEnumerable<string> filtered = testList.Where(x => x == "Test1"); DoSomeWork(filtered); DoSomeMoreWork(filtered); } public static void DoSomeWork(IEnumerable<string> items) { foreach (var item in items) { Console.WriteLine("do some work"); } } public static void DoSomeMoreWork(IEnumerable<string> items) { foreach (var item in items) { Console.WriteLine("do some more work"); } } 

I am right that this leads to the fact that two elements in the "filtered" are repeated twice, but actually the elements in the "testList"? So, given that "testList" was a large list with 10,000 elements, and the "filtered" reduced it to 10 elements, it would be smarter to "filter" the list (otherwise use var and just add ToList () at the end)

EDIT: This is the most embarrassing question I have ever asked. I knew that it would be bad to repeat IQueryable, for example, because it would result in retrieving the data twice from the database. However, I was not exactly sure about the memory lists. I would delete the question if I can ;-)

+6
source share
4 answers

The list is ultimately inherited from IEnumrable by checking this

 public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 

One more thing you can create an IEnurable list using the following avaialble constructor

 public List( IEnumerable<T> collection ) 

EDIT

The resulting filter list is repeated twice in two ways, but adds the .ToList () method to the end.

 IEnumerable<string> filtered = testList.Where(x => x == "Test1").ToList(); 

http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx

0
source

A large list will be repeated twice. To make sure this is really happening, change your request to:

 List<string> testList = new List<string> { "Test", "Test1", "Test1" }; IEnumerable<string> filtered = from t in testList where t == "Test1" select t; 

If you then set a breakpoint on the "where t ==" part of Test1, you will see that the debugger falls into this line for both iterations.

+7
source

A large list will be repeated twice. if you do not want this, you can β€œmaterialize” the constraint.

 var filtered = testList.Where(x => x == "Test1").ToList(); 

And there are many answers that tell you this. You should better search :-)

+1
source

it would be smarter to β€œfilter” the list (otherwise use var and just add ToList () at the end)

Yes, especially in terms of Linq-To-Entities, etc.

Returning IEnumerable with linq allows for different execution. When you add ToList() at the end, your list will be returned right there.

See http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

0
source

Source: https://habr.com/ru/post/899675/


All Articles