c# DataTable 查询

  using (SqlConnection con = new SqlConnection(ConStr)) {
                try
                {
                    string sql = "select * from mainUser with(nolock) where 1=1";
                    con.Open();
                    DataTable userDt = new DataTable("userInfoDt");
                    SqlDataAdapter dap = new SqlDataAdapter(sql, con);
                    dap.Fill(userDt);
                    con.Close();
                    // 通过AsEnumerable() 实现linq查询接口
                    var data = userDt.AsEnumerable();
                    //linq条件查询
                    data = data.Where(a=>a.Field<string>("uName")=="张三");
                    //linq结果集赋给新的DataTable
                    DataTable dd = data.CopyToDataTable();
                    //输出结果集
                    foreach (DataRow dr in data)
                    {
                        Console.WriteLine(dr["uid"]);
                        Console.WriteLine(dr["uName"]);
                        Console.WriteLine(dr["uPhone"]);
                        
                    }
                    //输出转DataTable 后的结果
                    foreach (DataRow item in dd.Rows)
                    {
                        Console.WriteLine(item["uid"]);
                        Console.WriteLine(item["uName"]);
                        Console.WriteLine(item["uPhone"]);
                        
                    }
                    
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    con.Close();
                }
             

            }

### 如何在C#中使用DataTable进行查询 #### 使用LINQ查询DataTable 可以通过将`DataTable`转换为可枚举对象来利用强大的LINQ查询功能。这使得能够编写简洁而表达力强的查询语句。 ```csharp var query = from row in dataTable.AsEnumerable() where row.Field<int>("ID") > 10 select new { Id = row.Field<int>("ID"), Name = row.Field<string>("Name") }; ``` 此方法允许开发者以声明式的风格构建复杂的过滤逻辑并轻松访问数据表中的字段[^1]。 #### 利用DataRow的方法直接操作 除了LINQ之外,还可以通过遍历`Rows`集合以及调用特定于行实例的方法来进行更传统的处理方式: ```csharp foreach(DataRow row in dataTable.Rows){ Console.WriteLine($"Id:{row["ID"]}, Value={row["Value"]}"); } ``` 这种方式提供了对每一行记录的细粒度控制,在某些场景下可能更加直观易懂。 #### 结合Expression Trees实现动态查询 对于需要支持运行时指定条件的情况,则可以考虑采用表达式树(Expression Tree),它允许程序创建和编译Lambda表达式作为数据结构而不是硬编码的形式存在。 ```csharp ParameterExpression pe = Expression.Parameter(typeof(DataRow), "r"); MemberExpression me = Expression.Property(pe, typeof(DataRow).GetProperty("Item")); ConstantExpression ce = Expression.Constant(5); BinaryExpression be = Expression.GreaterThan(me, ce); MethodCallExpression call = Expression.Call( typeof(Queryable), "Where", new[] {dataTable.AsEnumerable().AsQueryable().ElementType}, Expression.Constant(dataTable.AsEnumerable()), Expression.Lambda(be, new ParameterExpression[]{pe}) ); var result = ((IEnumerable<DataRow>)call.Compile().DynamicInvoke()).ToList(); ``` 上述代码片段展示了如何构造一个简单的比较运算符用于筛选大于给定数值的所有行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值