DataSet Compute MAX Value
Disclaimer : I am new to C# and WPF and adding features to existing code.
I am facing a problem while calculating the MAX value of db colums.
I have a Student database with table Sutent_DB
which has StudentID,StudentName,StudentClass I have to calculate the MAX of SudentID(ie:Number)
That I am doing it from DataSet.Table[Student_DB].Compute("MAX(StudentID)","")
Which is returing a Number Object.(Working Fine)
Now I want to restrict my selection to only to selected StudentClass=5
ie : DataSet.Table[Student_DB].Select("StudentClass=5");
Here I am f开发者_StackOverflow中文版acing a problem to calculate the MAX(StudentID) as SELECT returns DATAROW
The solution in my find is
int iMax=0;
foreach ( DataRow oneNewrow DataSet.Table[Student_DB].Select("StudentClass=5"))
{
if iMax < oneNewrow["StudentID"]
iMax = oneNewrow["StudentID"] ;
}
///use iMax here.
Just want to check any better or simple solution for my problem.
The second parameter of Compute
is a filter:
int maxId = (int)DataSet.Table[Student_DB].Compute("MAX(StudentID)","StudentClass=5");
You can use Linq to Objects on the array of DataRows returned from your query. Here's an example:
int iMax = DataSet.Table[Student_DB].Select("StudentClass=5")).Max(row => row["StudentID"]);
精彩评论