如何从LINQ内部联接查询结果集中填充DataTable?

时间:2012-05-02 11:28:49

标签: c# linq-to-sql datatable

我有一个从内连接表中选择的结果集 LINQ。我怎样才能将结果集作为DataTable返回?

DataProviderDataContext db = new DataProviderDataContext();

IEnumerable<DataRow> asd = (IEnumerable<DataRow>)(from dsa in (from usrAddr in db.T_USER_ADDRESSes join usr in db.T_USERs on usrAddr.USER_ID equals usr.USER_ID where usr.USER_ID == UserID select new { usr.REALNAME, usrAddr.USER_ID, usrAddr.USER_ADDRESS_ID, usrAddr.ADDRESS, usrAddr.TITLE, usrAddr.IS_DELIVERY, usrAddr.IS_INVOICE }).AsEnumerable() select dsa);

DataTable dt = asd.CopyToDataTable<DataRow>();

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

不幸的是CopyToDataTable<DataRow>()IEnumerable<DataRow>进行操作。您需要粉碎返回的数据,如下所示:http://msdn.microsoft.com/en-us/library/bb669096.aspx

答案 1 :(得分:0)

我很惊讶DataTable dt = asd.CopyToDataTable<DataRow>();没有为你做这份工作。

陷阱>

asd.CopyToDataTable(dt, LoadOption.PreserveChanges);