C# LINQ-将LINQ select转换为数据表
我有一个类对数据进行验证。我想从另一个地方重用这个类。 我不能改变那门课。 它是这样的:C# LINQ-将LINQ select转换为数据表,c#,.net,linq,dataset,C#,.net,Linq,Dataset,我有一个类对数据进行验证。我想从另一个地方重用这个类。 我不能改变那门课。 它是这样的: public static List<string> Validate(DataTable CurrentMasterTable, Dictionary<string, string[]> LocalMasterRuleSet) {...} 公共静态列表验证(DataTable CurrentMasterTable、Dictionary LocalMasterRuleSet
public static List<string> Validate(DataTable CurrentMasterTable, Dictionary<string, string[]> LocalMasterRuleSet)
{...}
公共静态列表验证(DataTable CurrentMasterTable、Dictionary LocalMasterRuleSet)
{...}
就其本身而言,它工作得很好
问题如下。从项目的另一个地方开始,为了使用该类,我必须向它发送DataTable对象,这样它就可以发挥它的魔力
如何使用LINQ,在表上进行选择并将其作为DataTable发送?
我从以下代码开始:
var listOfSinglePropertyNames = (from x in dbContext.tbl_mytable.AsEnumerable() select x);
IEnumerable<tbl_mytable> query = listOfSinglePropertyNames;
var listOfSinglePropertyNames=(从dbContext.tbl\u mytable.AsEnumerable()中的x选择x);
IEnumerable query=listOfSinglePropertyNames;
如何将查询转换为DataTable对象?在阅读其他帖子时,我发现我应该使用CopyToDataTable()
但是我没有取得任何成功。使用LINQ到数据表。根据您的需要调整此代码:
// 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>();
//查询SalesOrderHeader表中的订单
//2001年8月8日之后。
IEnumerable查询=
从orders.AsEnumerable()中的order开始
其中order.Field(“OrderDate”)>newdatetime(2001,8,1)
选择订单;
//从查询中创建一个表。
DataTable boundTable=query.CopyToDataTable();
我修改了这个问题,以表明我在发布之前做了研究:)似乎与。。如果您跳过AsEnumerable()
,那么其他问题的答案是否可行?