为什么我没有得到.CopyToDataTable()在LINQ查询() [英] Why am I not getting .CopyToDataTable() in Linq Query()

查看:82
本文介绍了为什么我没有得到.CopyToDataTable()在LINQ查询()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这下code例子是从MSDN 这里借。我没有得到query.CopyToDataTable()可以在我的code。 (见我的下面code中的注释行)。

 公共静态布尔SetPhysicianAsNotonServer(DataTable的DT)
        {
            DataTable的dtPhysicianServer = DT;
            数据表dtPhysicianClient = GetPhysicianClient();            VAR的查询=
                从SPhysician在dtPhysicianServer.AsEnumerable()
                加入CPhysician在dtPhysicianClient.AsEnumerable()
                在SPhysician.Field<串GT(PhysicianNumber)等于
                    CPhysician.Field<串GT(PhysicianNumber)
                新选择
                {
                    PhysicianNumber = CPhysician.Field<串GT(PhysicianNumber)
                 };            数据表FilterDt =查询; //query.CopyToDataTable();
            //没有这样做code HERE
            返回true;
        }


解决方案

您select语句返回一个字符串(的IEnumerable℃的序列串> IQueryable的<串GT; ),而不是数据行的序列。 CopyToDataTable()仅适用于的IEnumerable< T> ,其中T为或派生的DataRow

而不是中选择新的{...} - 这只会让你的类型的新序列,请尝试:

 选择CPhysician;

哪些应该返回CPhysician行所需序列

修改
如果您希望在非数据表,衍生的T转换为一个DataTable,MSDN有体现出任何类型和执行转换的样本类。

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

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).

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;
        }

解决方案

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.

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.

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.

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

这篇关于为什么我没有得到.CopyToDataTable()在LINQ查询()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆