CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    11

    LINQ queries and extension methods

    Hi Hivemind,

    I've just worked my way through a textbook chapter on LINQ and there are two points I'm still unsure of that the book hasn't covered very well.

    1) The book explains that extension methods such as Where() and Select() for IEnumerable<T> collections give the same results as using that same collection in a LINQ query, i.e.

    Code:
    IEnumerable<T> result =
    from name in myCollection
    where myCollection.myMember == value
    select name;
    would return the same result as

    Code:
    IEnumerable<T> result = myCollection.Where(name => name.myMember == value);

    Are these two options (LINQ queries or extension methods) always equally good for a given collection? I understand they'll always return the same result but are there situations where one would be better than the other, i.e. faster execution, less resources used, other reasons? Are extension methods just provided as an alternative because the same query can be sometimes be written in less code (as above), or can they give other advantages in some situations? If so, can you give me an example of such a situation? If not, why has C# provided two ways to do exactly the same thing? Wouldn't it have been better to just provide one way? The textbook hasn't adequately explained to me what the justification would be for using an extension method instead of a standard LINQ query.


    2) A join query in LINQ can be used to query several different data sources simultaneously and return the results as one collection, i.e.

    Code:
    var result = 
    from name1 in collection1
    join name2 in collection2 on join_condition1
    join name3 in collection3 on join_condition2
    join name4 in collection4 on join_condition3
    select new {member1 = name1, member2 = name2, member3 = name3, member4 = name4;
    The above uses four different data sources. If I use the Join() extension method instead of this query, am I limited to only two data sources in one method call? Visual C# 2008 seems to state that this Join() method for collection1 has four parameters, where the first is the second data source to use (collection2), the second and third arguments are the two quantities to use as the two operands in the join condition and the fourth argument gives the projection for the results (i.e. the select clause).

    If I choose to use the Join() extension method, am I limited to only querying the first two data sources with:

    Code:
    var result = 
    collection1.Join(
        collection2,
        name1 => name1.member1,
        name2 => name2.member2,
        (name1, name2) => new {member3 = name1, member4 = name2});
    or is there some way of querying collection1, collection2 and collection3 in one Join() method call on collection1?

    If not, would I have to use something like:

    Code:
    var result = collection1.Join(collection2, other_args)
        .Join(collection3, other_args)
        .Join(collection4, other_args);
    As a C# n00b I may not have explained myself very well here.

    Thanks
    Last edited by LibertyOrDeath; July 15th, 2009 at 09:40 AM.

  2. #2
    Join Date
    Feb 2009
    Location
    Atlanta, GA
    Posts
    17

    Re: LINQ queries and extension methods

    1) Yes. It is a matter of convenience and readability for calling the methods. Some people don't like to use lambda operators and might favor the former.

    2) As far as I know you have to call .Join(...).Join(...).

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: LINQ queries and extension methods

    Are these two options (LINQ queries or extension methods) always equally good for a given collection?
    LINQ queries are always translated to calls of extensions methods, so of this point of view they are equivalent, althought the compiler would propably produce other chain of extension method call.

    But extension methods are much more versatile than LINQ expression. There are constructs, which you cannot write with LINQ expressions, but with extension methods you can.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured