Thursday, January 20, 2011

Linq To Objects in C#

, LINQ to Objects means that we can use LINQ to query objects in a collection.We can query any type of object that implements the IEnumerable interface or IEnumerable, which is of generic type. Lists, arrays, and dictionaries are some collection objects that can be queried using LINQ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace linq
{
public class student
{
public int id { get; set; }
public string name { get; set; }
public string fname { get; set; }
public List<int> scores;
}
class Program
{
static List<student> students = new List<student>
{
new student{id = 1,name = "sumair",fname ="irshad",scores = new List<int>{90,80,70,60}},
new student{id = 2,name = "JIMM",fname = "CARRY",scores = new List<int>{90,30,30,30}},
new student { id = 3 ,name = "ANDREW", fname = "HEJISBERG",scores = new List<int>{90,90,90,90}},
new student { id = 4 ,name = "BILL",fname = "GATES",scores = new List<int>{80,10,10,10}}

};


static void Main(string[] args)
{
var getscorequery = from student in students
where student.scores [0]>=90
select student;

foreach (student s in getscorequery)
{
Console.WriteLine("{0},{1}", s.id, s.name);
}
Console.ReadKey();
}

}
}

OUTPUT:

1, SUMAIR
2, JIMM
3, ANDREW