LinQ

Last post 03-04-2008, 12:22 PM by Khaled Mahmoud. 0 replies.
Sort Posts: Previous
  • LinQ

     03-04-2008, 12:22 PM

    • Joined on 07-18-2007
    • Giza, Egypt
    • Posts 6
    • Top 25 Contributor

    LinQ is the most interresting feature in C# 3.0. I used it to make queries over database, list and any other enumerable objects with the highest performance. I'll give an example:

    Consider the class student as:

    public class Student

    {

    private Guid _Id;

    private int _Year;

    public Guid Id

    {

    set { _Id = value; }

    get { return _Id; }

    }

    public int Year

    {

    set { _Year = value; }

    get { return _Year; }

    }

    public Student(Guid id, int year)

    {

    Id = id;

    Year = year;

    }

    }

    and we have a list of students: List<Student> student = GetStudentsFromDatabase();

    Old code for searching for students in the 4th year (works under .net 2.0 and higher):

    List<Student> wantedStudents = new List<Student>();

    foreach (Student student in students)

    {

    if (student.Year == 4)

    wantedStudents.Add(student);

    }

    return wantedStudents;

    The new Code for searching using LinQ:

    var wantedStudentsQuery = from student in students

                                                       where student.Year == 4

                                                       select student;

    return wantedStudentsQuery.ToList<Student>();

     

    LinQ searching is SQL like code and so easy to understand and with much better performance.

    LinQ also can be used to retrieve records form database and supports all database operations. try it for better performance and for easy coding.


    Khaled Moawad
View as RSS news feed in XML





© All rights are reserved