Sunday, August 8, 2010

Linq to Objects - Arithmetic Operations

Linq to objects can have arithmetic operations like

  • Min
  • Max
  • Average
  • Sum
  • Count etc....
This is the List collection we are going to use....

public List GetStudents()
{
return new List
{
new Student{Name="Smilu",Address="EKM",PhNo="987654",flag=1},
new Student{Name="JK", Address="Parur",PhNo="897643",flag=0},
new Student{Name="Binu", Address="EKM",PhNo="798654",flag=1}
};
}

--------------------------------------------------

For using this we need to first create a var variable and assign the list into it...

var StudentInfo=GetStudents();

LabelMaxValue.Text = st.Max(s => s.flag).ToString();
LabelMin.Text=st.Min(s=>s.flag).ToString();
LabelCount.Text = st.Count.ToString();
LabelSum.Text = st.Sum(s => s.flag).ToString();
LabelAverage.Text = st.Average(s => s.flag).ToString();

Here you can see like s=> s.flag. This is a method showing the specific field we need to work on.



No comments: