为什么我没有在Linq Query()中获得.CopyToDataTable()

时间:2022-04-16 20:54:17

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。我没有在我的代码中获得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;
        }

6 个解决方案

#1


21  

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 或IQueryable ),而不是一系列DataRows。 CopyToDataTable()仅在IEnumerable 上可用,其中T是或者是从DataRow派生的。

Instead of select new { ... } - which will just get you a new sequence of that type, try:

不要选择新的{...} - 这将只为您提供该类型的新序列,请尝试:

select CPhysician;

Which should return the desired sequence of CPhysician rows.

哪个应该返回所需的CPhysician行序列。

Edit If 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有一个反映任何类型并执行转换的示例类。

http://msdn.microsoft.com/en-us/library/bb669096.aspx

http://msdn.microsoft.com/en-us/library/bb669096.aspx

#2


6  

It exists in a specific namespace are you importing it?

它存在于特定的命名空间中,您导入它吗?

System.Data.DataTableExtensions.CopyToDataTable() 

Also confirm the addition of this reference

同时确认添加此参考

System.Data.DataSetExtensions 

#3


3  

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 的定义:

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扩展DataRow

#4


2  

You need to reference the System.Data.DataSetExtensions assembly and use the System.Data namespace.

您需要引用System.Data.DataSetExtensions程序集并使用System.Data命名空间。

#5


0  

Have you referenced System.Data.DataSetExtensions assembly? This extension method is defined there.

您是否引用了System.Data.DataSetExtensions程序集?这里定义了这种扩展方法。

#6


0  

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。

#1


21  

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 或IQueryable ),而不是一系列DataRows。 CopyToDataTable()仅在IEnumerable 上可用,其中T是或者是从DataRow派生的。

Instead of select new { ... } - which will just get you a new sequence of that type, try:

不要选择新的{...} - 这将只为您提供该类型的新序列,请尝试:

select CPhysician;

Which should return the desired sequence of CPhysician rows.

哪个应该返回所需的CPhysician行序列。

Edit If 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有一个反映任何类型并执行转换的示例类。

http://msdn.microsoft.com/en-us/library/bb669096.aspx

http://msdn.microsoft.com/en-us/library/bb669096.aspx

#2


6  

It exists in a specific namespace are you importing it?

它存在于特定的命名空间中,您导入它吗?

System.Data.DataTableExtensions.CopyToDataTable() 

Also confirm the addition of this reference

同时确认添加此参考

System.Data.DataSetExtensions 

#3


3  

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 的定义:

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扩展DataRow

#4


2  

You need to reference the System.Data.DataSetExtensions assembly and use the System.Data namespace.

您需要引用System.Data.DataSetExtensions程序集并使用System.Data命名空间。

#5


0  

Have you referenced System.Data.DataSetExtensions assembly? This extension method is defined there.

您是否引用了System.Data.DataSetExtensions程序集?这里定义了这种扩展方法。

#6


0  

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。