.. of ones and zeros ..
a discussion of technology and software development by bryan napier

My First Linq Program

June 18th, 2007 . by bryan

I successfully coded my very first LINQ program today, and man is it a doozie!  Observe:

class Program
{
    static void Main(string[] args)
    {
        List<Student> studentList = new List<Student>();

        studentList.Add(new Student(“Charlie”, “Brown”));
        studentList.Add(new Student(“Drew”, “Carrie”));

        IEnumerable<Student> students =
            from
                Student
            in
                studentList
            where
                Student.LastName.Equals(“Brown”)
            select
                Student;

        foreach (Student student in students)
        {
            Console.WriteLine(student.FirstName);
        }

        Console.ReadLine();
    }
}

internal class Student
{
    private string _firstName;
    private string _lastName;

    public Student(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }
}

 Which when run looks like:

CropperCapture[3]

I’m excited by the possibilities this offers, and am anxious to have an opportunity to explore all LINQ has to offer!


Leave a Reply

Name

Mail (never published)

Website