Kuntady Nithesh
Kuntady Nithesh

Reputation: 11721

Convert this code to LINQ?

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 ?

Upvotes: 0

Views: 967

Answers (3)

StriplingWarrior
StriplingWarrior

Reputation: 156459

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 if statement 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, or FirstOrDefault to get it out of the resulting collection.

 var gradeValues = 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");

var firstGradeValue = gradeValues.First(); // will throw exception if there were no matches.
Console.WriteLine(firstGradeValue);

Upvotes: 4

Christoph Fink
Christoph Fink

Reputation: 23093

Try the following:

var grades = from r in GraceTables.Rows
             where obtMarksRow.Field<decimal>("Percentage") >= 
                     r.Field<decimal>("EXG_MARKS_ABOVE") && 
                   obtMarksRow.Field<decimal>("Percentage") <= 
                     r.Field<decimal>("EXG_MARKS_BELOW")
             select r.Field<string>("EXG_GRADE_NAME");

Upvotes: 1

DoctorMick
DoctorMick

Reputation: 6793

You shouldn't use Linq per se but you should use the DatasetExtensions, brought in alongside linq, to get your columns from the DataRow in a type safe way without the need to convert them, i.e.

if (obtMarksRow.Field<decimal>("Percentage") >= (Convert.ToDecimal(gradeCount["EXG_MARKS_ABOVE"])) && etc...

Upvotes: 0

Related Questions