Limit the return of the list.

I would like to constrain the return of this code:

Listx.AddRange(suggestions.Where(x => x.Contains(content)));


so that less than 7 items are added. tried it this way but it doesn't feel good and its rather slow because listx contains up to 100 entries.

Listx.AddRange(suggestions.Where(x => x.Contains(content)&&Listx.Count <= 6));


Anyone have any hints to improve the performance of the second piece of code? It is used every time the text field change event is fired, so it shouldn't delay input.


source to share

1 answer

You can use the Enumerable method. Take to limit the results of any Linq query.

Listx.AddRange(suggestions.Where(x => x.Contains(content)).Take(6));


If I were, you were familiar with 101 Samples for Linq , there you will find examples for every operation available in the Linq environment.



All Articles