Sorry, Im just learning LINQ and am relatively new at it.
Is it possible to convert the following into LINQ?
foreach (DataRow gradeCount in GraceTable.Rows)
{
if (Convert.ToDecimal(obtMarksRow["Percentage"]) >=
(Convert.ToDecimal(gradeCount["EXG_MARKS_ABOVE"])) &&
(Convert.ToDecimal(obtMarksRow["Percentage"]) <=
Convert.ToDecimal(gradeCount["EXG_MARKS_BELOW"])))
{
string Grade = Convert.ToString(gradeCount["EXG_GRADE_NAME"]);
}
}
Edit : sorry i missed for each loop in ma query and obtMarksRow comes from one more loop which is outside this
I wrote the query like this
var gradeValue = from DataRow gradeRow in GraceTable.Rows
let marksAbove = gradeRow.Field<decimal>("EXG_MARKS_ABOVE")
let marksBelow = gradeRow.Field<decimal>("EXG_MARKS_BELOW")
where obtMarksRow.Field<decimal>("Percentage") >= marksAbove && obtMarksRow.Field<decimal>("Percentage") <= marksBelow
select gradeRow.Field<string>("EXG_GRADE_NAME");
but i am getting the value (gradeValue.ToString() ) as “System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Data.DataRow,System.String]”
Whats wrong ?
No, it isn’t possible. As the commenters point out, LINQ is for querying collections of things. You don’t appear to have a collection here: just an
ifstatement and an assignment.Furthermore, be careful about trying to convert things to LINQ unnecessarily. As you start to understand LINQ better, you’ll find yourself naturally using it for a variety of purposes. But starting off with the assumption that code will be better with LINQ is probably a fallacy.
Edit
As mentioned earlier, LINQ is about querying a collection for a set of results. If you only want one result, you can use
Single,First,SingleOrDefault, orFirstOrDefaultto get it out of the resulting collection.