Questions tagged [tolist]

125 questions
27
votes
5 answers

How to convert LINQ query result to List?

I need to convert linq query result to list. I tried the following code: var qry = from a in obj.tbCourses select a; List lst = new List(); lst = qry.ToList(); The following error occurred for the above code:…
Brillia
  • 1,243
  • 2
  • 13
  • 21
22
votes
4 answers

Does foreach execute the query only once?

I have a list of items and a LINQ query over them. Now, with LINQ's deferred execution, would a subsequent foreach loop execute the query only once or for each turn in the loop? Given this example (Taken from Introduction to LINQ Queries (C#), on…
Marcel
  • 13,233
  • 18
  • 77
  • 130
15
votes
1 answer

Calling ToList() on ConcurrentDictionary while adding items

I've run into an interesting issue. Knowing that the ConcurrentDictionary is safely enumerable while being modified, with the (in my case) unwanted side-effect of iterating over elements that may disappear or appear multiple times, I…
10
votes
5 answers

Rules of thumb for when to call ToList when returning LINQ results

I'm looking for rules of thumb for calling ToList/ToArray/MemoizeAll(Rx) on IEnumerables, as opposed to returning the query itself when returning IEnumerable of something. Often I find that it is better to just return the query and let the caller…
Markus Johnsson
  • 3,741
  • 18
  • 30
10
votes
3 answers

List shows 4 items in debugger even if filled with exactly one element

When I look at list populated with single item in debugger its _items field contains 4 elements. Can you explain the behavior? I've found that while debugging my console application to learn about Distinct and ToList and result confuses me.…
user70267
  • 103
  • 5
8
votes
2 answers

How to convert IEnumerable> to List?

I really don't understand this T thing yet. I need to convert below result to List private void generateKeywords_Click(object sender, RoutedEventArgs e) { string srText = new TextRange( txthtmlsource.Document.ContentStart, …
MonsterMMORPG
  • 20,310
  • 69
  • 183
  • 306
6
votes
6 answers

Is .NET ObservableCollection<> ToList() thread safe? If not, how to proceed

I have an ObservableCollection of Logs that I have bound to my GUI through a property public ObservableCollection Logs {get; private set;} There is a requirement to show a subset of the logs somewhere else so I have: public…
Dave
  • 6,585
  • 11
  • 51
  • 92
6
votes
4 answers

To ToList() or not to ToList()?

Given an in memory (not LINQ to SQL) list of classes: List myItems = /*lots and lots of items*/; which I am grouping using a GroupBy() statement: myItems.GroupBy(g => g.Ref) and then immediately consuming in a foreach loop is there any…
Liam
  • 22,818
  • 25
  • 93
  • 157
6
votes
1 answer

DropDownList From Objects MVC

Ok so I want make a DropDownList from my object list here is my getting object method public List GetCategoriesList() { BaseShopEntities context = new BaseShopEntities(); List uniqCategories = (from c in context.Category …
Dox
  • 158
  • 1
  • 10
6
votes
3 answers

Why, the first query.ToList() work, but the second not work?

my problem is with the ToLinq() method : I don't understind why the first request work without problem, but the second give me an exception like : (the node type of the LINQ expression arrayIndex n is not supported in LINQ to Entities) …
5
votes
2 answers

java IntStream cannot use collect(Collectors.toList()), compilation error, why?

As below: IntStream iStream = IntStream.range(1,4); iStream.forEach(System.out::print); List list1 = iStream.collect(Collectors.toList());//error! Java 1.8 compiler gives type deduction error. Similar code could work for String type: …
Hind Forsum
  • 8,077
  • 8
  • 40
  • 88
5
votes
1 answer

Linq ToList() fails to trigger Immediate Execution

I am having a very peculiar problem: the ToList() extension method is failing to convert results to a list. here is my code, standard boilerplate linq query, I converted ToList() twice for good measure var assets = new List(); using (var ctx…
franklores
  • 365
  • 3
  • 14
4
votes
2 answers

Difference between "ToListAsync()" and "AsAsyncEnumerable().ToList()"

Function need to return Task> Following both options are returning Task>, which one is more efficient? Is there any standard way here? Option 1 : Task> GetRecords() { return …
4
votes
2 answers

linq orderby.tolist() performance

I have an ordering query to a List and calling for many times. list = list.OrderBy().ToList(); In this code ToList() method is spending high resources and takes very long time. How can I speed up with another ordering method without converting back…
Baran
  • 1,298
  • 5
  • 16
  • 32
4
votes
3 answers

When IQueryable returns no record ToList() throws an exception

dataContext.Geo_Countries.Where(c => c.Name.Contains(searchKey)).ToList(); when the IQueryable returns no records I get a value null exception. What is the solution?
Doozie
  • 41
  • 1
  • 2
1
2 3
8 9