如何在LINQ中选择表行数?

时间:2013-12-23 11:09:42

标签: c#

如何使用LINQ获取表条件行计数,我从数据库中获取一个表,我想知道哪些列 name =“title” value =“一个“行号?

这是我不完美的代码:

                  var query=(from result1 
                              in ds.Tables[0].AsEnumerable()
                              where result1.Field<string>("title")=="A"
                              select result1);

如果我必须预先查询以获取计数!

4 个答案:

答案 0 :(得分:2)

无需复制相同的查询代码,最后添加.Count()方法以获取计数。 walkhard的解决方案是最好的,与我自己使用的技术相同。基本上如下:

var query=(from result1 
                              in ds.Tables[0].AsEnumerable()
                              where result1.Field<string>("title")=="A"
                              select result1);

int iCount = query.Count();

答案 1 :(得分:1)

在查询上使用Count()来计算

var count=(from result1 
      in ds.Tables[0].AsEnumerable()
      where result1.Field<string>("title")=="A"
      select result1).Count();

答案 2 :(得分:1)

使用内置Count

int count = query.Count();

答案 3 :(得分:1)

试试这个

var countOfRows=(from result1 
                          in ds.Tables[0].AsEnumerable()
                          where result1.title=="A"
                          select result1).Count();
相关问题