Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio
This is the forum to discuss the Wrox book ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solution by Vincent Varallo; ISBN: 9780470396865
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 29th, 2009, 11:27 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Page 107 converting to vb.net

Hello,

I'm trying to re-write your examples using vb.net. I'm stuck on page 107 trying tp create the lambda function and trying to sort the generic list.

Here's my code so far of the public function SortByPropertyname:
Code:
Public Function SortByPropertyName(ByVal propertyName AsString, ByVal ascending AsBoolean) As List(Of T)
 
Dim param As ParameterExpression
Dim body As Expression
Dim sortExpression As Expression(Of T)
 
param = Expression.Parameter(GetType(T), "N")
body = Expression.Convert(Expression.Property(param, propertyName), GetType(Object))
sortExpression = Expression.Lambda(body, param)
 
If ascending Then...here's where I'm stuck
End IfEnd Function
There are several things that I'm not 100% sure about. 1) If I've created the lambda function correct...and 2) I have no clue on how to do the orderby.

Thanks for your help and I'm really enjoying reading this book, the concepts are what I thought they should be, but could not put my finger on it until reading this book.

I will keep searching and if I do find my answer I will post it.

-realkewl
  #2 (permalink)  
Old March 29th, 2009, 11:41 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi realkewl,

Your'e not far off. Try this:

vb Code:
Public Function SortByPropertyName(ByVal propertyName As String, ByVal ascending As Boolean) As List(Of T)
    'Create a Lambda expression to dynamically sort the data.
    Dim param As ParameterExpression
    Dim body As Expression
    Dim sortExpression As Expression(Of T)

    param = Expression.Parameter(GetType(T), "N")
    body = Expression.Convert(Expression.Property(param, propertyName), GetType(Object))
    sortExpression = Expression.Lambda(Of Func(Of T, Object))(body, param)

    If ascending Then
        Return Me.AsQueryable(Of T)().OrderBy(Of T, Object)(sortExpression).ToList(Of T)()
    Else
        Return Me.AsQueryable(Of T)().OrderByDescending(Of T, Object)(sortExpression).ToList(Of T)()
    End If
End Function

If you ever want to convert between c# and vb, I find this site gets it right most of the time.

HTH
Phil
  #3 (permalink)  
Old March 29th, 2009, 02:05 PM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Sweet, thanks for the quick reply and the link. I tried the code and I get an error on this line:

Code:
 
sortExpression = Expression.Lambda(Of Func(Of T, Object))(body, param)
Error message:
cannot be converted to system.linq.expressions.expression(Of T)

Here's the class declaration and my imports maybe that's not correct:

Code:
 
imports System
Imports System.Collections.Generic
Imports System.Linq.Expressions
Imports System.Data.Linq
 
<Serializable()> _
Public MustInherit Class ENTBaseBOList(Of T As ENTBaseBO)
Inherits List(Of T)
-realkewl
  #4 (permalink)  
Old March 29th, 2009, 05:30 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

No it's my fault, your class and import are fine. Sorry, I haven't had a chance to look at lambda expressions and Linq properly as yet, so I am flying a little blind myself.

The OrderBy method expects a Func delegate, so I believe the sortExpression should be declared as
vb Code:
Dim sortExpression As Expression(Of Func(Of T, Object))

There may also be an issue with the AsQueryable parts (maybe my quick reply was a little too hasty), but I can't check properly right now. I'll try and have a look tomorrow morning, unless you or somewhere else can figure it out beforehand...

Phil
  #5 (permalink)  
Old March 30th, 2009, 03:45 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Here we go, this compiled for me:
vbnet Code:
Public Function SortByPropertyName(ByVal propertyName As String, ByVal ascending As Boolean) As List(Of T)
        'Create a Lambda expression to dynamically sort the data.
        Dim param As ParameterExpression
        Dim body As Expression
        Dim sortExpression As Expression(Of Func(Of T, Object))

        param = Expression.Parameter(GetType(T), "N")
        body = Expression.Convert(Expression.Property(param, propertyName), GetType(Object))
        sortExpression = Expression.Lambda(Of Func(Of T, Object))(body, param)

        If ascending Then
            Return Me.AsQueryable().OrderBy(Of Object)(sortExpression).ToList()
        Else
            Return Me.AsQueryable().OrderByDescending(Of Object)(sortExpression).ToList()
        End If
    End Function

List(Of T) already knows about typeparam T (obviously) so the AsQueryable, OrderBy etc extension methods don't need it to be specified.

Phil
The Following User Says Thank You to philip_cole For This Useful Post:
realkewl (March 30th, 2009)
  #6 (permalink)  
Old March 30th, 2009, 09:26 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Phil,

Thanks, I'll give it a try this morning. This is really cool stuff, but breaks from the norm. Really, appreciate the help.

-realkewl
  #7 (permalink)  
Old March 30th, 2009, 09:33 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Cool code worked, sweet. Some how, as I continue to read, I think I will need your help again. :-)

-realkewl
  #8 (permalink)  
Old March 30th, 2009, 09:42 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

Thanks for working through this guys, hope its working out for you.

Vince
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help, converting vb.net few lines to C# nesrine ASP.NET 2.0 Professional 1 March 5th, 2007 07:10 AM
Problem Converting C# to VB.NET in ASP.NET kwilliams ASP.NET 2.0 Basics 0 February 9th, 2007 06:22 PM
Converting C# to VB.net ninel General .NET 0 August 11th, 2006 12:36 PM
converting code from c# to VB.net drachx General .NET 0 October 20th, 2004 11:19 PM
converting DTS package from VB 6 to VB .NET petroleo Pro VB Databases 0 August 18th, 2003 05:01 PM



All times are GMT -4. The time now is 04:44 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.