如果使用LINQ C#将列名作为参数,如何查找指定的列及其行值 [英] How to find specified column and its row value if column name as parameter using LINQ C#
本文介绍了如果使用LINQ C#将列名作为参数,如何查找指定的列及其行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据表和列名称,如1000,2000,3000,4000,...... 10000
当我将参数作为列名称传递为1000时,它应该找到列并返回值。
例如,
我的参数是1000(列名)
var Qry =(来自r中的tbl1.AsEnumerable()
其中r.Field< double>(1000)== EMPNUM
选择新的
{
})。Count();
最后我想获得计数和我需要查询相同以获取列名1000的值
i尝试上述查询,但是在r.Field<的地方得到错误; double>(1000)==>错误
如何在C#中编写LINQ Auery
我尝试了什么:
var Qry =(来自r中的tbl1.AsEnumerable()
其中r.Field< double> ;(1000)== EMPNUM
选择新的
{
})。Count();
解决方案
你正在调用访问字段 index 的重载,而不是字段 name 。
如果你想读取具有指定名称的字段,你需要传递一个字符串:
其中 r.Field< double>( 1000)== EMPNUM
如果你想计算匹配行的数量:
int count = tbl1.AsEnumerable()。Count(r = > r.Field< double>( 1000)== EMPNUM);
如果要检索匹配的行:
IEnumerable的<&的DataRow GT; rows = tbl1.AsEnumerable()。其中(r = > r.Field< double>( 1000)== EMPNUM);
如果列名是: 1000,2000,3000,4000,.... 10000并且您想将其传递给查询,请检查:
var colName =1000;
var cnt = tbl1.AsEnumerable()
.Select(r => r.Field< double>(colName))
.Count();
Hi, I have datatable and column name something like "1000,2000,3000,4000,....10000" When i pass the parameter as "1000" as column name it should find the column and return the values. For Example, My parameter is "1000" (Column name) var Qry = (from r in tbl1.AsEnumerable() where r.Field<double>(1000) == EMPNUM select new { }).Count(); Finally i want to get the count and same as i need query to get the values for column name 1000 i tried the above query, but getting error in where r.Field<double>(1000) ==> Error How to write the LINQ Auery in C#
What I have tried:
var Qry = (from r in tbl1.AsEnumerable() where r.Field<double>(1000) == EMPNUM select new { }).Count();
解决方案
You're calling the overload which access the field index, not field name.
If you want to read the field with the specified name, you need to pass a string:
where r.Field<double>("1000") == EMPNUM
If you want to count the number of matching rows:
int count = tbl1.AsEnumerable().Count(r => r.Field<double>("1000") == EMPNUM);
If you want to retrieve the matching rows:
IEnumerable<DataRow> rows = tbl1.AsEnumerable().Where(r => r.Field<double>("1000") == EMPNUM);
If the name of column is: "1000,2000,3000,4000,....10000" and you want to pass it to the query, check this:
var colName = "1000"; var cnt = tbl1.AsEnumerable() .Select(r=>r.Field<double>(colName)) .Count();
这篇关于如果使用LINQ C#将列名作为参数,如何查找指定的列及其行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文