Hi,
How to find minimum and maximum value and filter row by row based on the parameter.
1. I have data as follow:
DataTable dt = new DataTable();
dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)"), new DataColumn("FROM"),new DataColumn("TO"),new DataColumn("SUB E(MIN)"), new DataColumn("SUB E(MAX)") });
dt.Rows.Add(101, "A", "100", "80", "200", "200", "550", 20, "100", "200", "500");
dt.Rows.Add(101, "B", "100", "90", "300", "270", "200", 20, "100", null, null);
dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", 20, "100", "100", "100");
dt.Rows.Add(101, null, "100", "200", "300", "100", "200", 20, "100", null, null);
2. I have two input strings, which i have to "convert" into filter criteria:
string string1 = "GRADE#SUB B";
string string2 = "SUB C=100#SUB D=250#SUB E=300";
First i want to get the row which contains in the range between within 100 ((i.e) SUB C (MIN) and SUB C (MAX)) (take
string2
and split the string and take first in that. SUB C=100).
It should return two records:
101,"A", "100", "100", "50", "200", "250", "700", 20, "100"
101, "B", "100", "150", "70", "250", "200", "100", 20, "100"
3. After that, with in the above two records, i want to filter one more condition need to check. This is also need to check within Min and Max of SUB D=300.
It should return only one record:
101,"A", "100", "100", "50", "200", "250", "700", 20, "100"
4. In that, finally i want to get the records for the field of GRADE and SUB B.
So the final output would be
GRADE=A
SUB B=100
Note : Filter and check row within the row using
string2
and display the result in the form of
string1
.
pls. let us know if you need any more details.
The output i want either simple C# or Linq Query..???
What I have tried:
DataTable dt = new DataTable();
dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)"), new DataColumn("FROM"),new DataColumn("TO"),new DataColumn("SUB E(MIN)"), new DataColumn("SUB E(MAX)") });
dt.Rows.Add(101, "A", "100", "80", "200", "200", "550", 20, "100", "200", "500");
dt.Rows.Add(101, "B", "100", "90", "300", "270", "200", 20, "100", null, null);
dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", 20, "100", "100", "100");
dt.Rows.Add(101, null, "100", "200", "300", "100", "200", 20, "100", null, null);
string string1 = "GRADE#SUB B";
string string2 = "SUB C=100#SUB D=250#SUB E=300";
string[][] subCD = string2.Split('#').Select(s => s.Split('=')).ToArray();
<pre>
string[] condition = string2.Split('#');
string filter = "";
foreach (string subcondition in condition)
{
if (!String.IsNullOrEmpty(filter))
{
filter += " AND ";
}
string[] subconditions = subcondition.Split('=');
filter += "[" + subconditions[0] + "] = '" + subconditions[1] + "'";
}
var result = dt.Select(filter);
string[] fields = string1.Split('#');
string Grade = result.FirstOrDefault().Field<string>(fields[0]);
string SUBB = result.FirstOrDefault().Field<string>(fields[1]);