C# 为什么我在 Linq Query() 中没有得到 .CopyToDataTable()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1595350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I not getting .CopyToDataTable() in Linq Query()
提问by Jango
This following code example is borrowed from MSDN here. I am not getting query.CopyToDataTable() available in my code. (see the commented line in my following code).
下面的代码示例是从 MSDN here借来的。我的代码中没有提供 query.CopyToDataTable() 。(请参阅我以下代码中的注释行)。
public static bool SetPhysicianAsNotonServer(DataTable dt)
{
DataTable dtPhysicianServer = dt;
DataTable dtPhysicianClient = GetPhysicianClient();
var query =
from SPhysician in dtPhysicianServer.AsEnumerable()
join CPhysician in dtPhysicianClient.AsEnumerable()
on SPhysician.Field<string>("PhysicianNumber") equals
CPhysician.Field<string>("PhysicianNumber")
select new
{
PhysicianNumber = CPhysician.Field<string>("PhysicianNumber")
};
DataTable FilterDt = query; //query.CopyToDataTable();
//YET TO DO CODE HERE
return true;
}
采纳答案by Rex M
Your select statement is returning a sequence of strings (IEnumerable<string>
or IQueryable<string>
), not a sequence of DataRows. CopyToDataTable() is only available on IEnumerable<T>
where T is or derives from DataRow.
您的 select 语句返回一个字符串序列(IEnumerable<string>
或IQueryable<string>
),而不是一个 DataRows 序列。CopyToDataTable() 仅适用于IEnumerable<T>
T 是 DataRow 或从 DataRow 派生的地方。
Instead of select new { ... }
- which will just get you a new sequence of that type, try:
而不是select new { ... }
- 这只会为您提供该类型的新序列,请尝试:
select CPhysician;
Which should return the desired sequence of CPhysician rows.
这应该返回所需的 CPhysician 行序列。
EditIf you wish to convert a non-datatable-derived T to a datatable, MSDN has a sample class that reflects out any type and performs the conversion.
编辑如果您希望将非数据表派生的 T 转换为数据表,MSDN 有一个示例类可以反映任何类型并执行转换。
回答by Shankar R10N
It exists in a specific namespace are you importing it?
它存在于特定的命名空间中,您要导入它吗?
System.Data.DataTableExtensions.CopyToDataTable()
Also confirm the addition of this reference
还确认添加了这个参考
System.Data.DataSetExtensions
回答by Darin Dimitrov
You need to reference the System.Data.DataSetExtensions
assembly and use the System.Data
namespace.
您需要引用System.Data.DataSetExtensions
程序集并使用System.Data
命名空间。
回答by elder_george
Have you referenced System.Data.DataSetExtensions assembly? This extension method is defined there.
您是否引用了 System.Data.DataSetExtensions 程序集?这个扩展方法在那里定义。
回答by Arjan Einbu
Navigating further into MSDN online brings me to this page: http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.aspx
进一步浏览 MSDN 在线将我带到此页面:http: //msdn.microsoft.com/en-us/library/system.data.datatableextensions.aspx
It says its in the System.Data namespace (using System.Data) and you need to reference the System.Data.DataSetExtensions.dll.
它说它在 System.Data 命名空间中(使用 System.Data),您需要引用 System.Data.DataSetExtensions.dll。
回答by bruno conde
I think that's because your creating a anonymous type to hold the Field
object.
Try this:
我认为这是因为您创建了一个匿名类型来保存Field
对象。尝试这个:
var query = from SPhysician in dtPhysicianServer.AsEnumerable()
join CPhysician in dtPhysicianClient.AsEnumerable()
on SPhysician.Field<string>("PhysicianNumber") equals
CPhysician.Field<string>("PhysicianNumber")
select CPhysician;
DataTable FilterDt = query.CopyToDataTable();
Definition of CopyToDataTable<T>
:
的定义CopyToDataTable<T>
:
public static DataTable CopyToDataTable<T>(
this IEnumerable<T> source
)
where T : DataRow
So what you select with the query must be of type IEnumerable<T>
where T
extends DataRow
所以你选择的查询必须是类型IEnumerable<T>
where T
extendsDataRow
回答by Josh G
This issue for me was caused by using Dotnet Core 2.0, which appears to have no way to turn an IEnumerable
back into a DataTable
after it's converted.
对我来说这个问题是由使用 Dotnet Core 2.0 引起IEnumerable
的DataTable
,它在转换后似乎无法将back 变成 a 。
Just adding this for anyone that comes across this question with the same issue.
只需为遇到相同问题的任何人添加此内容即可。