Galin Iliev's blog

Software Architecture & Development

C# 3.0 and LINQ Tutorial

After speaking with my friend Marto I decided to write a tutorial about forthcoming features in new version of the very popular language C#.

As always there is a lack of development materials in Bulgarian so it is written in Bulgarian and there is no language barieer for bulgarian developers who wants to take a look behind the corner to new version of our favorite C#.

There is posibility to translate the whole tutorial in English (when it si finished) so this could be good starting point for all English speaking devs but this depends on the interest (and my availability).

I have posted first two parts (of total 13 so far): Introduction and Acknowledgment with LINQ and basic capabilities.

I hope I will be able to post next part very soon (projected within next two weeks) and I will announce it here.

In the meantime I will be very grateful for your feedback. Any comments, reccomendations and request for more detailed reviews are welcome. You can post them as comments on the blog or to my emails stated on here.

Thank you

Comments (4) -

  • Angel

    2/3/2007 10:27:43 PM | Reply

    Hello Galcho,

    I think it's great that you've wrote those tutorials in Bulgarian. I read the one about C# 3.0 and LINQ and I find it informing. Good job!

    I noticed that you've made small typing mistake giving the same example twice at this place:

    Имплицитно типизиране на масивите, в зависимост от данните
    int[] arr=new int[] { 1, 2, 3 }

    Best wishes,

  • Galcho

    2/4/2007 11:26:01 AM | Reply

    Hi Angel,

    I am glad you found the tutorial useful. I think next parts will be more interesting Smile

    Thank you for pointing me at the duplicate example. It is fixed now.

    Best Regards,

  • Angel

    2/4/2007 9:00:30 PM | Reply

    Hi Galcho,

    I read the second part and it gave me good understanding on what LINQ is about. I am total newbee in LINQ. Looking at the employee example:

    var result = from e in employees
    group e by e.Department into g
    select new {
    Department = g.Key,
    Employees = g,
    SumSalaries = g.Sum(e => e.Salary)};

    does result need to be var? That should be new feature for C#3.0 but if it could be of type Employee[] it is more readable, or am I wrong? The part starting with "select new { ..." looks confusing to me and I didn't quite understand what's going on there, perhaps you should elaborate more on that. Also, the "from" keyword is somewhat confusing (at least to me), I wonder why MS didn't use something like "foreach" there!

    Other than that it looks very good.

  • Galcho

    2/5/2007 10:03:13 AM | Reply

    Hi Angel,

    This part was just aknowledgement with LINQ and C# 3.0 so I skipped detailed explanations. These will be put in one of last parts after I describe new features.

    I will start answering you in reverse Smile

    from or foreach - by initial concept LINQ should be close to SQL. Having this in mind it is not strange they from keyword to point at datasource (as in T-SQL). Why not foreach (which has same meaning) - probably because it is already taken and I haven't seen language construction to perform two different tasks.

    "select new { ..." part  - this returns new type (anonymous type) with three members (Department, Employees, SumSalaries ).

    and now the first one - "does result need to be var?" because of anonymous types result is not collection of type Employee anymore. There is one more complication - using group by operator creates new type "g". With simple queries it is possible to write

    IEnumerable<string> result = from s in contacts
                             where s.StartsWith("G")
                             select s;

    but this will easily get broken when you extend LINQ query

    Best regards,