隐藏列表< DataTable> C#中的许多DataTable [英] Covert List<DataTable> to Many DataTable in C#

查看:111
本文介绍了隐藏列表< DataTable> C#中的许多DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,它是根据一个数据集中的EmailID列分组的。以下是我对分组

 List< DataTable>的查询result = dsgetTimesheetlist.AsEnumerable()
.GroupBy(row = > row.Field< string>( TeamLeaderEmailID))
。选择(g = > g.CopyToDataTable ())
.ToList();





我得到了我预期的结果。但现在我想将这些组拆分成许多数据表。比如,一组电子邮件ID到一个数据表,另一组电子邮件ID到另一个数据表。

解决方案

一种方法是不将结果转换为名单。从结尾删除ToList调用,并将结果作为数据表。



之后使用Distinct,它们的键列获取所有不同的值并通过它们。在每次迭代中,基于数据视图创建一个新的数据表。



所以伪代码就像(不编译):

  foreach  string  email 结果。选择(x = >  x.Field(  email))。Distinct()){
result.DefaultView.RowFilter = email =' + email + ');
datatablelist.Add(result.DefaultView.ToTable());
}



作为附注,如果将数据拆分为多个数据表,则无法轻松查询和操作所有行。由于这种分裂,数据分成几部分仅在极少数情况下才有用。如果原因是在UI中显示较小的数据部分,您可以随时使用过滤器。



链接:

- DataView.RowFilter [ ^ ]

- DataView.ToTable [< a href =https://msdn.microsoft.com/en-us/library/a8ycds2f(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]


I have list which is grouped by EmailID column from one dataset. Below is my query for grouping

List<DataTable> result = dsgetTimesheetlist.AsEnumerable()
                           .GroupBy(row => row.Field<string>("TeamLeaderEmailID"))
                           .Select(g => g.CopyToDataTable())
                           .ToList();



I am getting result which i expected. But Now I want to split those groups into Many datatables. Like, one group of email id to one datatable, another group of emailID to another datatable.

解决方案

One way would be that you don't cast the results as a list. Remove the ToList call from the end and let the result be a data tables.

After that using Distinct for they key column fetch all the distinct values and lop through them. In each iteration create a new data table based on a dataview.

So in pseudo code something like (not to be compiled):

foreach (string email in result.select(x => x.Field("email")).Distinct()) {
  result.DefaultView.RowFilter = "email = '" + email + "'");
  datatablelist.Add(result.DefaultView.ToTable());
}


As a side-note if you split the data into multiple data tables you loose the ability to easily query and manipulate all rows together. Because of this splitting the data into parts is useful only in quite rare situations. If the reason is showing smaller portions of data in UI you can always use filters for that.

Links:
- DataView.RowFilter[^]
- DataView.ToTable[^]


这篇关于隐藏列表&lt; DataTable&gt; C#中的许多DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆