简体   繁体   中英

Convert a datatable to IList c#

i got a code for converting datatable to ilist. which may work but the code not very clear to me. here is code

table.AsEnumerable()
.Select(r => table.Columns.ToDictionary(c => c.ColumnName, c => r[c]))
.ToList();

what is table.Columns.ToDictionary()...what it does. what is the meaning of c => c.ColumnName, c => r[c] what r[c] return. not very clear to me.

i got a another snippet....here it is

var list = new List<DataRow>();
foreach (var row in table.Rows)
   list.Add(row);

return list;

this code is clear to me but i want to know which code has good performance out of 2 different snippet. please discuss. thanks

The ToDictionary call converts each DataRow to a Dictionary<string, object> which you can use to find the value of any field within the row. => is the syntax used in a lambda expression - you should really learn about lambda expressions if you're going to do any significant amount of work with LINQ.

Your second snippet would be more simply written as:

var list = table.Rows.Cast<DataRow>().ToList();

In terms of efficiency - the first form fetches all the data from the row as it builds the dictionary. That's likely to make it less efficient if you only need to look at some of the fields. On the other hand, it may be more convenient. It depends on what you do with it later. In terms of memory, the second form will mean the DataRow objects themselves can't be garbage collected, whereas the first form doesn't need the rows afterwards - but it's created a copy of all the actual data. Whether that's relevant in your situation will again depend on the rest of your code.

I wouldn't focus on the performance to start with though - start off with whichever code makes the rest of your application simplest to write, then check whether that performs well enough .

Well to put it simply its a lambda expression that translates into a method. r is an object and c is its indexer. that means you can select one of r's array values. Example: from table R return me column number 8 so c will have the value of 8. Hope it helps.

They are different in their results.

Short:

  • The first statement creates a list of dictionaries, where each key-value pair is column name and value. The ToDictionary extension method expects two parameters: a key and a value. The key is in this example the column name ( c => c.ColumnName ). Because this method is invoked on the Columns -collection of your datarow, c is a column. The second parameter takes the column name and inserts it into the current row, giving the actual value for the column. So your dictionary gets filled with KeyValuePairs consisting of ColumnName->Value. This is done for each row. Afterwards the ToList() function returns a list of dictionaries.
  • The second example just fills a list with the DataRows, the result has no advantage over using the Rows collection of the datatable.

If you use .Net Framework 3.5 then try this code:

DataTable dt = new DataTable();
var list = dt.Select().ToList();

The table.AsEnumerable() does very similar thing as entire SECOND code snippet and you can use either of them.

The code:

.Select(r => table.Columns.ToDictionary(c => c.ColumnName, c => r[c]))
.ToList();

creates dictionary with values for the each table row. You probably do not need this at all because the DataRow already has this functionality.

On the subject of code performance, I agree with previous posters that, at first you should not concern yourself with speed. Get it working, then get it right.

However, based on the (limited) research I've seen on benchmarks between a standard foreach loop and the LINQ fluent syntax, LINQ is only slightly slower than a standard foreach loop...and when dealing with small sets of data, it's an insignificant amount of slowness. I'm currently conducting some independent statistical analysis on this subject as time allows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM