2015-11-30 87 views
-2

我有一个对数据进行验证的类。我想从另一个地方重用这个类。 我不能改变那个类。 它去是这样的:LINQ - 将LINQ select转换为DataTable

public static List<string> Validate(DataTable CurrentMasterTable, Dictionary<string, string[]> LocalMasterRuleSet) 
    {...} 

在自己的它工作得很好。

问题在下面。从项目中的另一个地方,为了使用该类,我必须将它发送给DataTable对象,以便它可以发挥它的魔力。

我该如何使用LINQ,在表上作出选择并将其作为DataTable发送? 我有这样的代码开始:

var listOfSinglePropertyNames = (from x in dbContext.tbl_mytable.AsEnumerable()            select x); 

     IEnumerable<tbl_mytable> query = listOfSinglePropertyNames; 

如何转换的查询DataTable对象?阅读其他帖子,我已经看到我应该使用CopyToDataTable();

但我没有任何成功。

+0

我已经修改了的问题,以显示我张贴:) –

+0

之前所做的研究似乎是重复[这个问题](http://stackoverflow.com/questions/ 1526726/linq-to-sql-convert-iqueryable-to-dataset)..如果你跳过'AsEnumerable()'也许其他问题的答案可以工作? – Default

回答

1

使用LINQ to datatable。适应此代码为您needings:

// Query the SalesOrderHeader table for orders placed 
// after August 8, 2001. 
IEnumerable<DataRow> query = 
from order in orders.AsEnumerable() 
where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1) 
select order; 

// Create a table from the query. 
DataTable boundTable = query.CopyToDataTable<DataRow>();