C#怎样在DataTable中使用linq查询

时间:2025-03-12 07:54:48

本文新版已迁移到我的新博客,查看更多技术文章请访问我的博客


当前有一个任务,需要将datatable中数据筛选后存入新的datatable中,我的mysql数据库中表字段名为ErrorType,程序如下

 

	    SqlDataSource errorHoleInfoSqlDataSource = new SqlDataSource(); //还是使用SQLDataSource数据源,参见我的其他文章
             = "localhost_Connection";
             = "errorHoleInfoSqlDataSource";

             = "selectQuery2";
             = "select * from errorholeinfo where ProjectName = '" + projectName + "'";
            (query);
            ();

            ITable src = ["selectQuery2"];
            DataTable table = new DataTable("project");

            foreach (IColumn column in )
                (, );
            foreach (IRow row in src)
                (());


            if ( == 0)
            {
                return false;
            }
            else
            {
                DataTable newDataTable = new DataTable();
                ("ErrorType", typeof(string));

                 var errorHoleInfoQuery = from r in ()                //使用linq进行查询
                                          where <string>("ErrorType") == "漏孔"
                                          let objectArray = new object[] 
                                          {
                                                <string>("ErrorType")
                                           }
                                          select objectArray;    SqlDataSource errorHoleInfoSqlDataSource = new SqlDataSource(); //还是使用SQLDataSource数据源,参见我的其他文章
             = "localhost_Connection";
             = "errorHoleInfoSqlDataSource";

             = "selectQuery2";
             = "select * from errorholeinfo where ProjectName = '" + projectName + "'";
            (query);
            ();

            ITable src = ["selectQuery2"];
            DataTable table = new DataTable("project");

            foreach (IColumn column in )
                (, );
            foreach (IRow row in src)
                (());


            if ( == 0)
            {
                return false;
            }
            else
            {
                DataTable newDataTable = new DataTable();
                ("ErrorType", typeof(string));

                 var errorHoleInfoQuery = from r in ()                //使用linq进行查询
                                          where <string>("ErrorType") == "漏孔"
                                          let objectArray = new object[] 
                                          {
                                                <string>("ErrorType")
                                           }
                                          select objectArray;
 	    	foreach (var array in errorHoleInfoQuery)//存入新的DataTable
                 {
                     (array);
                 }        
	    	foreach (var array in errorHoleInfoQuery)//存入新的DataTable
                 {
                     (array);
                 }        
		return true;return true;
	    }    }