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 Query
- Why does LINQ not work to add DataRows to a DataTable?
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- Why Linq Prepend() does not work with List<T>?
- Why does .Equals not work in this LINQ example?
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- Why does DbFunctions not work with Linq to Entities
- Why orderBy does not work for this linq expression?
- Why does Linq join on Nullable<T> not work the same as ==?
- Why distinct in LINQ does not work for this case?
- Why does VB.NET Linq Lambda work one way but not the other
- Left join with linq query does not work with null columns in datatable
- Why my ordering by linq does not work
- Why this multi linq join code does not work?
- Why LINQ to Entities does not recognize the method 'System.String ToString()?
- Dude, where's my object? or, Why does Linq not return my object?
- C# 6 null conditional operator does not work for LINQ query
- Order by does not work with Concat() in LINQ
- Why does not null in LINQ query still return null records?
- Why does EF 5.0 not support this EF 4.x LINQ syntax when compiling to sql?
- Why does Linq not have Head and Tail?
- Why does a GC after a LINQ query free the WhereListIterator but not the Func representing the condition?
- Why does using anonymous type work and using an explicit type not in a GroupBy?
- LINQ: why does this query not work on an ArrayList?
- My Enumerable class does not work with Linq statements like .where in c#
- Why does the Linq OrderBy extension not affect the list it is called on?
- Nhibernate LINQ DateTime.AddDay does not work
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
- Linq :DataTable select does not work if column name has space in it?
- LINQ Except() Method Does Not Work
- LINQ to Entities Include() does not work
More Query from same tag
- How to find key value pair in a dictionary with values > 0 with key matching a certain string pattern?
- Join Error | Unable to create a constant value of type only primitive
- LINQ Select From One Generic List to Assign Value to Another
- LINQ query on DataGridViewSelectedCellCollection
- Should repositories implement IQueryable<T>?
- ExpressionTree creation for y => ConstantCollection.Any(x => y.Contains(x)). Different scope parameterExpressions
- C# LINQ : Dynamically create property and values that includes sum of a value from a grouped query
- Specific Elements from an XML document
- Returning a list of objects asynchronously
- LINQ to XML - Load XML fragments from file
- linq member has no supported translation to SQL
- find inside array of objects
- Select 2 tables using LINQ
- Linq query to select only common items in two lists
- How can I return a single value integer from Linq from an array of objects that have other nested values
- Multi-dimensional arrays in Linq
- Can I do a LINQ select with .Select without havng to use a class object
- Use method in entity framework query
- Fill items List in List from another List
- Entity Framework - How to return strongly typed IQueryable with joined entities
- Querying a complex Json with Linq
- Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32,
- How to update XML attributes using Linq to XML in C#
- Is there a LINQ-oriented method to quicky evaluate nested lists in dictionaries
- Transform IEnumerable to DataTable to its primitive types
- Unable to create a constant value of type 'IdentityUserRole'. Only primitive types or enumeration types are supported
- What is the difference between First and FirstOrDefault , Last and LastOrDefault
- Inner join not working when use equal with %, What an alternative way to use it like like %
- Entity Framework query decorator sends query immediately
- Querying an IQuery from a cached list returns null exception