none
Convertion of LINQ query syntax to method syntax

    Question

  • Hello,

            I have this LINQ query syntax but I'm having a hard time converting it to a method type syntax.

    ---
    query syntax

    Dim myBlog = From blog In _db.Blogs _
    Order By blog.bsdate Descending _
    Where blog.bPublish = True _
    Let commentCount = (From a In blog.Blog_Comments Where a.bCmtPublish = True Select a).Count()

    ---

    It is the Let clause I can't get below while converting. :(

    ---
    method syntax

    _db.Blogs.OrderByDescending(Function(p) p.bsdate).Where(Function(c) c.bPublish = True)
    ---



    cheers,
    imperialx      
    Saturday, March 07, 2009 5:22 AM

Answers

  • 'Let' is just another form of Select.  It just re-selects the current element and whatever was in the Let clause. 

    db.Blogs.OrderByDescending(Function(p) p.bsdate).Where(Function(c) c.bPublish = True)
                  .Select(Function(c) New With {c, commentCount = (From a In blog.Blog_Comments Where a.bCmtPublish = True Select a).Count()})

    Of course, you'll now have to manually 'unpack' the result of this select to continue referencing 'c' and 'commentCount'.  The query syntax does this for you.
    Wayward LINQ Lacky
    Sunday, March 08, 2009 7:21 PM

All replies

  • Hello,

    The let keyword is not an extensibility method and therefore cannot be called as a method. I believe it only exists as a reserved word parsed by the compiler as far as I know so in order to use it with LINQ you will have to use the query syntax you describe.



    Sunday, March 08, 2009 3:24 PM
  • 'Let' is just another form of Select.  It just re-selects the current element and whatever was in the Let clause. 

    db.Blogs.OrderByDescending(Function(p) p.bsdate).Where(Function(c) c.bPublish = True)
                  .Select(Function(c) New With {c, commentCount = (From a In blog.Blog_Comments Where a.bCmtPublish = True Select a).Count()})

    Of course, you'll now have to manually 'unpack' the result of this select to continue referencing 'c' and 'commentCount'.  The query syntax does this for you.
    Wayward LINQ Lacky
    Sunday, March 08, 2009 7:21 PM
  • Nice, Ive learnt something new as well then. Thank you Matt.

    It seems like some extra effort is required to use the method syntax for this. I use the method syntax cause (and when) it saves effort over the query syntax.

    Good to know. Cheers
    Monday, March 09, 2009 4:30 AM
  • Hi Matt,

                I understand. And one thing, I miss some line in my query syntax. Sorry about that.

    ---
                Dim myBlog = From b In db.Blogs _
                       Order By b.bsdate Descending _
                       Let CommentCount = (From a In b.Blog_Comments _
                       Where a.bCmtPublish = True Select a).Count() _
                       Where b.bPublish = True _
                       Select b.ID, b.Title, CommentCount
    ---

               And I try completing the statement but I get this error "DataBinding: 'System.String' does not contain a property with the name 'Title'." under my template below.

    ---
            <ItemTemplate>
                <div class="post">
                    <h2 class="postTitle">
                        <a href="<%#Eval("Title")%>.aspx">
                            <%#Eval("Title")%></a></h2>

                Dim myBlog = db.Blogs.OrderByDescending(Function(p) _
                    p.bsdate).Where(Function(c) _
                    c.bPublish = True).Select(Function(d) _
                    New With {d, .CommentCount = (From a In db.Blog_Comments _
                    Where a.bCmtPublish = True Select a).Count()}).Select _
                    (Function(j) j.d.Title
    )
    ---
     

              What did I do wrong? :(

    cheers,
    imperialx

    • Edited by imperialx Monday, March 09, 2009 12:16 PM Typo
    Monday, March 09, 2009 12:12 PM
  •  You are selecting a System.String there. Instead you'll need to select an object with a Title property:

    (Function(j) New With { .Title = j.d.Title })
    Wednesday, March 11, 2009 2:55 PM