加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – DataTable上的LINQ查询 – 找不到查询模式的实现

发布时间:2020-12-16 00:04:21 所属栏目:百科 来源:网络整理
导读:任何人都可以告诉我为什么这不编译?错误是: Could not find an implementation of the query pattern for source type System.Data.DataTable . Where not found. using System;using System.Collections.Generic;using System.Linq;using System.Text;usi
任何人都可以告诉我为什么这不编译?错误是:

Could not find an implementation of the query pattern for source type System.Data.DataTable. Where not found.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace caLINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "Data Source=STEVEN-PCSQLEXPRESS;Initial Catalog=linqtest;Integrated Security=True;Pooling=False";
            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
            {
                 connection.Open();
                 String cmdText = "select * from customers";
                 System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(cmdText,connection);
                 System.Data.SqlClient.SqlDataReader dr;
                 dr = cmd.ExecuteReader();
                 DataSet ds = new DataSet();
                 ds.Load(dr,LoadOption.OverwriteChanges,"tab1");
                 DataTable dt = ds.Tables[0];
                 var qy = from c in dt // error is here
                          where c.country == "Italy"
                          select c.name;
            }
            Console.ReadKey();
        }
    }
}

解决方法

尝试:

var qy = from c in dt.AsEnumerable()
         where c.Field<string>("country") == "Italy"
         select c.Field<string>("name");

AsEnumerable()将返回IEnumerable< DataRow>可以与LINQ一起使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读