Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,

C#
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

C#
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..?
Posted
Updated 9-Aug-17 20:20pm
v2

1 solution

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:

C#
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
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900