LINQ - 如果我的列名称为LIST参数列,如何查找特定列 [英] LINQ - how to find particular column if my column name as LIST parameter column

查看:45
本文介绍了LINQ - 如果我的列名称为LIST参数列,如何查找特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将列表值传递到数据表中。



我想首先找到该列,然后检查该列是否存在该值。



我想在LINQ查询中写为bool true / false



例如,



< pre lang =c#> string str ABSCHECK = ABS A#ABS B // 输入值

string [] words1 = str.Split(' #'); // 拆分值

words1 [ 0 ] ABS A // list





[1] ABS B



以上两个值是一个列名,我想签到下表。



DTTABLE:



ABS A(MN)ABS A(MX)ABS B(MN)ABS B(MX)ABS C(MN)ABS C(MX)

100 100 100 100 100 100 100

ABS A ==>匹配两列和值(如ABS A(MN)和ABS A(MX))



对于ABS B ==>匹配两列和值(如ABS B(MN)和ABS B(MX))



所以我想返回结果是真的。



如果没有匹配,则表示(未标识列且值不存在)返回false。



如何编写LINQ ..?



以下是我的查询



  string  str ABSCHECK = ABS A#ABS B  //  输入值 

string [] words1 = str.Split(' #'); // 拆分值

bool results =( from r in DTTABLE.AsEnumerable()
< span class =code-sdkkeyword> select r.Field< string>(words1.ToList())); // 查找列和值





但不工作..?



我尝试过:



 string str ABSCHECK = ABS A#ABS B //输入值

string [] words1 = str.Split('#'); //分割值

bool results =(来自DT in DTTABLE.AsEnumerable()
select r.Field< string>(words1.ToList())); //查找列和值

但是没有工作..?

解决方案

不确定我理解你很好,但是......如果你想获得单列结果,你必须遍历你从字符串中拆分的列的名称。看看例子:



 DataTable dt =  new  DataTable( ); 

dt.Columns.Add( new DataColumn( ABS A typeof string )));
dt.Columns.Add( new DataColumn( ABS B typeof string )));

dt.Rows.Add( new object [] { 1});
dt.Rows.Add( new object [] { B 2\" });
dt.Rows.Add( new object [] { C 3\" });
dt.Rows.Add( new object [] { D 4\" });
dt.Rows.Add( new object [] { E 5\" });

string str = ABS A#ABS B; // 输入值

List< string> cols = str.Split( new string [] { },StringSplitOptions.RemoveEmptyEntries)。ToList(); // 拆分值

foreach string c in cols)
{
< span class =code-keyword> var
result = dt.AsEnumerable()
.Select(r = > r.Field< string>(c))
.ToList();
// result.Dump(); // LinqPad选项显示存储在结果变量中的值
}


Hi ,

I am passing list value into the datatable.

I want to find the column first and then check whether the value exist or not for that columns.

I want to write in LINQ Query as bool true/false

for example,

string str ABSCHECK = ABS A#ABS B // input value

string[] words1 = str.Split('#'); //Splitting the values

words1[0] ABS A //list



[1] ABS B

The above two value is a column name and i want to check in the below table.

DTTABLE :

A ABS A(MN) ABS A(MX) ABS B(MN) ABS B(MX) ABS C(MN) ABS C(MX)
100 100 100 100 100 100 100
For ABS A ==> Matching two columns and values present(like ABS A(MN) and ABS A(MX))

For ABS B ==> Matching two columns and values exists (like ABS B(MN) and ABS B(MX))

So i want to return result is true.

if nothing is matching, it means (column not identified and value not present) returns false.

How to write LINQ..?

below is my query

string str ABSCHECK = ABS A#ABS B // input value

string[] words1 = str.Split('#'); //Splitting the values

bool results = (from r in DTTABLE.AsEnumerable()
                           select r.Field<string>(words1.ToList())); // to find out columns and value



but not working..?

What I have tried:

string str ABSCHECK = ABS A#ABS B // input value

string[] words1 = str.Split('#'); //Splitting the values

bool results = (from r in DTTABLE.AsEnumerable()
                           select r.Field<string>(words1.ToList())); // to find out columns and value

but not working..?

解决方案

Not sure i understand you well, but... If you want to get single-column result, you have to loop through the names of columns you'd split from string. Take a look at example:

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ABS A", typeof(string)));
dt.Columns.Add(new DataColumn("ABS B", typeof(string)));

dt.Rows.Add(new object[]{"A", "1"});
dt.Rows.Add(new object[]{"B", "2"});
dt.Rows.Add(new object[]{"C", "3"});
dt.Rows.Add(new object[]{"D", "4"});
dt.Rows.Add(new object[]{"E", "5"});

string str = "ABS A#ABS B"; // input value
 
List<string> cols = str.Split(new string[]{"#"}, StringSplitOptions.RemoveEmptyEntries).ToList(); //Splitting the values

foreach(string c in cols)
{
	var result = dt.AsEnumerable()
              .Select(r=> r.Field<string>(c))
			  .ToList();
	//result.Dump(); //LinqPad option to display values stored in a result variable
}


这篇关于LINQ - 如果我的列名称为LIST参数列,如何查找特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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