从DataTable C#获取所有列表 [英] GET ALL LIST FROM DataTable C#
本文介绍了从DataTable C#获取所有列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有请帮助我,
我在下面有DataTable:
Dear All Please Help me,
I Have DataTable in the below:
===============
NO | Name |
---------------
1 | Jhon |
---------------
2 | Wayne |
---------------
3 | Robert |
i想要获取All列表并将其保存在变量sting中。
我使用此代码:
i want to take the All list and and save it in the variable sting.
i'm use this code :
sTagValue1 = cbxparameter.SelectedValue.ToString();
SqlDataAdapter da = new SqlDataAdapter(("select * from Table"), oSqlConn);
DataTable dt = new DataTable();
da.Fill(dtformid);
sVariable =dt.Rows[?][?].ToString();//this my problem....
推荐答案
您要求的内容与您的代码不符:如果您想要从DataTable中获取名称列表,那么如果使用Linq方法则非常简单:
What you are asking for doesn't really match your code: If you want to get a list of the Names from the DataTable, then that's pretty easy if you use Linq methods:
List<string> list = dt.AsEnumerable().Select(r => r["Name"].ToString()).ToList();
HTML编码,该死的![/ edit]
[edit]HTML encode, damn it![/edit]
不确定我是否正确理解你的问题,但如果你想得到一个包含所有名字的单个字符串变量(也许是分隔符),那么试试像:
Not sure if I understood your question correctly but if you want to get a single string variable containing all the names (with delimiter perhaps) then try something like:
string sVariable = string.Join(",", dt.AsEnumerable().Select(x => x["Name"].ToString()));
请记住使用
using System.Data;
以便能够使用LINQ方法。
in order to be able to use the LINQ methods.
这篇关于从DataTable C#获取所有列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文