如何在ado.net中按类型获取行数? [英] how to get row count by type in ado.net?
本文介绍了如何在ado.net中按类型获取行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在c#中有一个数据集。其中我有一些类型为a和b的行。现在我想计算ado.net中c#中每种类型的行数。
i have one dataset in c#. in which i have some rows with type a and type b. now i want to count the number of rows per type in c# in ado.net.
推荐答案
如果您的问题是这样,那么将适合您..
If your question is this , then will work for you..
DataTable dt = new DataTable();
dt.Columns.Add("Types", typeof(string));
dt.Rows.Add("TypeA");
dt.Rows.Add("TypeB");
dt.Rows.Add("TypeA");
int TypeACount = dt.Rows.OfType<DataRow>().Count(k => k["Types"].ToString() == "TypeA");
int TypeBCount = dt.Rows.OfType<DataRow>().Count(k => k["Types"].ToString() == "TypeB");
int rowCounter = 0;
foreach(DataRow dr in dts.Tables[0].Rows)
{
rowCounter++;
}
Response.Write("" + rowCounter);
希望你从这里得到基本的想法,这将迭代你的表的所有行并增加计数。
我只是打印计数,但你可以在任何你想要的地方使用!!
祝你好运!
Hope you get the basic idea from here, this will iterate all the rows of your table and increase the count.
I have simply printed the count, but you can use that wherever you want!!
Good Luck !
private DataSet SplitRows(DataTable source)
{
DataSet ds = new DataSet();
foreach(DataRow r in source.Rows)
{
DataTable copy = source.Clone();
copy.LoadDataRow(r.ItemArray, true);
ds.Tables.Add(copy);
}
return ds;
}
试试这个
这篇关于如何在ado.net中按类型获取行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文