具有多个选择列的数据表 [英] Datatable with multiple select columns

查看:57
本文介绍了具有多个选择列的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用select方法访问多列的datable。但iam返回值为

Iam accessing datable with multiple columns using select method. But iam returning value as

{System.Data.DataRow[0]}




DataRow[] row = datatablename.Select("KEY = '"+ SKEY + "' AND COLUMN_NAME = '"+ item[0].tostring() + "'");



i在datarow中需要的值[]并循环播放。



此代码有什么问题。



我尝试过:




i need value in datarow[] and loop through it.

What is the problem with this code.

What I have tried:

DataRow[] row = datatablename.Select("KEY = '"+ SKEY + "' AND COLUMN_NAME = '"+ item[0].tostring() + "'");

推荐答案

首先, tostring()应该是 ToString()。其次,你应该使用lambda表达式。只要您之前已验证 KEY COLUMN_NAME 列存在,以下代码应该可以工作(可能需要一些按摩)在数据集中。



First, tostring() should be ToString(). Second, you should be using a lambda expression. The following code should work (may need some massaging) as long as you previously verified that the KEY and COLUMN_NAME columns exist in the dataset.

var rows = datatable.Rows.Where(x => x.GetInt32("KEY") == SKEY && 
                                     x.GetString("COLUMN_NAME") == item[0].ToString());





您最终会得到符合指定条件的 IEnumerable DataRow 项集合,如果是null,则结果为null没有找到匹配。



我个人不喜欢直接使用数据表,而是喜欢将数据表内容移动到强类型对象列表中,因为事情这种方式更可控。但那只是我。



最后,尽量避免使用SQL保留字(例如 KEY )作为列名。它并没有真正伤害任何东西,但这是不好的做法。



You'll end up with an IEnumerable collection of DataRow items that meet the specified criteria, or a null result if no matches were found.

I personally don't like working directly with datatables, and instead prefer to move the datatable contents into a list of strongly typed objects because things are much more controllable that way. But that's just me.

Finally, try to avoid using SQL reserved words (such as KEY) for column names. It doen't really hurt anything, but it's bad practice.


这篇关于具有多个选择列的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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