score:1
Select Method
The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
So this select is never executed:
data.Select(r => dt.Rows.Add((r as IDictionary<string, object>).Values.ToArray()));
Reference http://msdn.microsoft.com/ru-ru/library/bb548891.aspx
score:0
I haven't tested this, but try soemthing like this instead:
data.Cast<Dictionary<string, object>>().ToList().ForEach(x => dt.Rows.Add(x.Values.ToArray()));
score:0
As sailor already pointed out, LINQ is lazily evaluated. Placing a .LastOrDefault()
on the end of your query will cause execution (because by trying to get the last element it will execute your Select()
), however it will make your code look even worse!
By definition, LINQ Select should not be used for side-effecting behaviour. I believe you should be able to see that in your question option 2 looks much cleaner than option 1. By reading option 2, I can easily understand, that you are adding each element of data
to the datatable. By reading option 1, I'd guess you're doing something to the data
variable, which would be wrong.
In conclusion, just stick with option 2. When performance is the same (as this is the case), try to make your code as "easy" to read as possible. A random person should be able to get the general idea without reading through the whole thing.
Source: stackoverflow.com
Related Articles
- Why does LINQ not work to add DataRows to a DataTable?
- Left join with linq query does not work with null columns in datatable
- Does LINQ work with IEnumerable?
- How does LINQ expression syntax work with Include() for eager loading
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- C# 6 null conditional operator does not work for LINQ query
- Order by does not work with Concat() in LINQ
- how does except method work in linq
- How does Linq work (behind the scenes)?
- Does this LINQ code perform multiple lookups on the original data?
- My Enumerable class does not work with Linq statements like .where in c#
- Nhibernate LINQ DateTime.AddDay does not work
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Linq :DataTable select does not work if column name has space in it?
- How does this linq code that splits a sequence work?
- LINQ Except() Method Does Not Work
- LINQ to Entities Include() does not work
- Why Linq Prepend() does not work with List<T>?
- Does LINQ work on Index?
- LINQ - dynamic orderby clause does not work
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- How does OrderBy in LINQ work (behind the scenes)?
- Why does .Equals not work in this LINQ example?
- How does linq Expression<TDelegate> assignment work on a language syntax level
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- Why does DbFunctions not work with Linq to Entities
- Linq OrderBy int property does not work
- creating Linq to sqlite dbml from DbLinq source code
- Stripping characters from PredicateBuilder to improve search accuracy
- Entity Framework LINQ equivalent of TSQL to include count of child records
- Very complex linq
- Monotouch JIT error on device with LINQ
- How to Select Multiple Fields from DataGridView using LINQ in C# WinForms
- Using Take() with bindingsource
- How do I do this SELECT statement that uses column renaming (or column alias) with LINQ?
- How to group the headers and add where condition inside the select statement
- Merge two XML files and add missing tags and attributes
- Find the number of pairs in an array whose difference is K?
- search my list help with linq
- Linq Exclude columns in children
- Linq merging results
- LINQ - filtering a result set and joining the result set
- How to call a function inside a where clause of IQueryable
- Grouping a range using linq
- Efficient way to determine collection has at least 2 items
- Efficiently filtering a large collection of POCO entities
- Sort a list by alpha values first, numeric values next
- query through five tables with nested Any operator