p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio (http://p2p.wrox.com/forumdisplay.php?f=477)
-   -   Page 107 converting to vb.net (http://p2p.wrox.com/showthread.php?t=73626)

realkewl March 29th, 2009 11:27 AM

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

philip_cole March 29th, 2009 11:41 AM

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

realkewl March 29th, 2009 02:05 PM

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

philip_cole March 29th, 2009 05:30 PM

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

philip_cole March 30th, 2009 03:45 AM

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

realkewl March 30th, 2009 09:26 AM

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

realkewl March 30th, 2009 09:33 AM

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

-realkewl

varallov March 30th, 2009 09:42 PM

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

Vince


All times are GMT -4. The time now is 09:32 AM.

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