简体   繁体   English

将数据表转换为IList c#

[英]Convert a datatable to IList c#

i got a code for converting datatable to ilist. 我得到了一个将数据表转换为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. 什么是table.Columns.ToDictionary()......它的作用。 what is the meaning of c => c.ColumnName, c => r[c] what r[c] return. c => c.ColumnName,c => r [c]是什么意思r [c]返回。 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. 这段代码对我来说很清楚,但我想知道哪个代码具有2个不同代码段的良好性能。 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. ToDictionary调用将每个DataRow转换为Dictionary<string, object> ,您可以使用它来查找行中任何字段的值。 => 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. =>lambda表达式中使用的语法 - 如果要对LINQ进行大量的工作,你应该真正了解lambda表达式。

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. 在内存方面,第二种形式意味着DataRow对象本身不能被垃圾收集,而第一种形式之后不需要行 - 但是它创建了所有实际数据的副本。 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. 好吧,简单地说它是一个lambda表达式,转换成一个方法。 r is an object and c is its indexer. r是一个对象,c是它的索引器。 that means you can select one of r's array values. 这意味着您可以选择r的数组值之一。 Example: from table R return me column number 8 so c will have the value of 8. Hope it helps. 示例:从表R返回列号8,因此c将具有值8.希望它有所帮助。

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. ToDictionary扩展方法需要两个参数:键和值。 The key is in this example the column name ( c => c.ColumnName ). 在这个例子中,关键是列名( c => c.ColumnName )。 Because this method is invoked on the Columns -collection of your datarow, c is a column. 因为在数据行的Columns -collection上调用此方法,所以c是一列。 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. 因此,您的字典将填充由ColumnName-> Value组成的KeyValuePairs。 This is done for each row. 这是针对每一行完成的。 Afterwards the ToList() function returns a list of dictionaries. 然后,ToList()函数返回一个字典列表。
  • The second example just fills a list with the DataRows, the result has no advantage over using the Rows collection of the datatable. 第二个示例只是使用DataRows填充列表,结果没有使用数据表的Rows集合的优势。

If you use .Net Framework 3.5 then try this code: 如果您使用.Net Framework 3.5,请尝试以下代码:

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. table.AsEnumerable()与整个SECOND代码片段非常相似,您可以使用它们中的任何一个。

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. 您可能根本不需要这个,因为DataRow已经具有此功能。

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. 然而,基于我在标准foreach循环和LINQ流畅语法之间的基准测试中看到的(有限的)研究,LINQ只比标准的foreach循环略慢......并且在处理小数据集时,它是一个微不足道的缓慢。 I'm currently conducting some independent statistical analysis on this subject as time allows. 我现在正在时间允许的情况下对这个主题进行一些独立的统计分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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