将IEnumerable <datarow>转换为EnumerableRowCollection <datarow>或DataView

时间:2015-08-27 14:08:11

标签: c# .net linq

我如何resultEnumerableRowCollection<DataRow>DataView

DataTable table1 = /*something*/ ;
DataTable table2 = /*something*/ ;
DataTable table3 = /*something*/ ;

IEnumerable<DataRow> result = (from t1 in table1.AsEnumerable() join 
                                    t2 in table2.AsEnumerable() on t1.Field<int>("id") equals t2.Field<int>("t1id") join
                                    t3 in table3.AsEnumerable() on t2.Field<int>("t3id") equals t3.Field<int>("id")
                               where 666.Equals(t3.Field<int>("id"))
                              select t1);

感谢

2 个答案:

答案 0 :(得分:1)

来自.NET文档here

DataTable orders = dataSet.Tables["SalesOrderHeader"];

EnumerableRowCollection<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true
    orderby order.Field<decimal>("TotalDue")
    select order;

DataView view = query.AsDataView();

bindingSource1.DataSource = view;

答案 1 :(得分:-1)

正如Jon Skeet所说:

DataTable table1 = /*something*/ ;
DataTable table2 = /*something*/ ;
DataTable table3 = /*something*/ ;

DataTable result = (from t1 in table1.AsEnumerable() join 
                                    t2 in table2.AsEnumerable() on t1.Field<int>("id") equals t2.Field<int>("t1id") join
                                    t3 in table3.AsEnumerable() on t2.Field<int>("t3id") equals t3.Field<int>("id")
                               where 666.Equals(t3.Field<int>("id"))
                              select t1).CopyToDataTable<DataRow>();

如果你想要一个DataView:

DataTable table1 = /*something*/ ;
DataTable table2 = /*something*/ ;
DataTable table3 = /*something*/ ;

DataView result = (from t1 in table1.AsEnumerable() join 
                                    t2 in table2.AsEnumerable() on t1.Field<int>("id") equals t2.Field<int>("t1id") join
                                    t3 in table3.AsEnumerable() on t2.Field<int>("t3id") equals t3.Field<int>("id")
                               where 666.Equals(t3.Field<int>("id"))
                              select t1).CopyToDataTable<DataRow>().AsDataView();