Hey, I didn't say it _can't_ be done. Internally, Dynamic LINQ does
something very similar to what you just wrote.

The thing is: should I write that code by myself all the time... or use a
library that somebody already tested?

Just like I use uNhAddIns.WPF.Collections.WpfCollectionTypeFactory (ahem)
and the associated types, I use DynamicQueryable to solve the sort
expression/direction parsing for me, including the step of resolving
TEntity, which is not known at compile time in this case. That's it.

   Diego


On Mon, Mar 8, 2010 at 09:24, José F. Romaniello <[email protected]>wrote:

> Just in case you don't understood the aggregate thing, and how to use the
> result..
>
>
> var parameter = Expression.Parameter(typeof(TEntity), "p");
>
> var body = parameter;
>
> foreach(var subProperty in propertyAccess.Split('.'))
> {
>   body = Expression.Property(body, subProperty);
> }
>
> var sortExpression = Expression.Lambda<Action<TEntity>>(body, parameter);
>
> var result = source.OrderBy(sortExpression);
>
>
> (actually the aggregate thing is resharper magic)
>
>
> 2010/3/8 José F. Romaniello <[email protected]>
>
> let me show you another way.
>>
>> Lets said that you have a grid to show Albums and you have one column with
>> this bindingpath: "Artist.Country.Name"
>>  then you want to sort the grid when the user press the column header, in
>> the database or memory... So you want this to be technology independent.
>>
>> So I do this:
>>
>> var parameter = Expression.Parameter(typeof(TEntity), "p");
>> var body = (MemberExpression)
>> propertyAccess.Split('.').Aggregate(parameter, Expression.Property);
>> Expression.Lambda<Action<TEntity>>(body, parameter);
>>
>>
>> where TEntity is Album
>> and propertyAccess is the binding path of the column.
>>
>> again, Why do you need dynamic linq?
>>
>>
>>
>>  2010/3/7 Diego Mijelshon <[email protected]>
>>
>>> José,
>>>
>>> Dynamic Linq has some legitimate uses.
>>> In the app we're working on, for example, clicking on any column on a WPF
>>> grid sorts by that column via NH. Instead of doing something by hand, we
>>> just grab the binding path, and apply a dynamic OrderBy to the source (NH
>>> IQueryable). The Dynamic LINQ library takes care of parsing the path into
>>> all the corresponding expressions.
>>>
>>>    Diego
>>>
>>>
>>> On Sun, Mar 7, 2010 at 14:05, José F. Romaniello <[email protected]
>>> > wrote:
>>>
>>>> dynamic linq is a ***.
>>>> Why do you wan that? Who defines that string?
>>>>
>>>> instead of concatenating strings to make your query you can create an
>>>> expression with:
>>>>
>>>> Expression.Parameter()
>>>> Expression.Property()
>>>> Expression....
>>>>
>>>>
>>>>
>>>>
>>>> 2010/3/7 Stefan Wenig <[email protected]>
>>>>
>>>> Hi
>>>>>
>>>>> so you want to use LINQ 2 objects (the IEnumerable version), not LINQ
>>>>> 2 NH?
>>>>> In any case, Dynamic LINQ only provides parsing of Where etc. clauses,
>>>>> not entire queries. So this is only a complete solution if the
>>>>> structure of your query is alreay known at compile time and users just
>>>>> enter conditions etc. DL is not using lamda notation, BTW, so I don't
>>>>> know how multiple sources are referred to in more complex queries
>>>>> (might not be supported at all, need to check).
>>>>> I'd try dynamic compilation, that shouldn't be too hard. (CodeDOM can
>>>>> help with compilation and even generate boiler plate code for any
>>>>> supported language.) Use the #line directive to get useful error
>>>>> messages. You have to take care of assembly loading though, and you
>>>>> can't unload them anymore. (There's been talk about straight type/
>>>>> assembly unloading support in .NET 4, but I didn't check). Using
>>>>> separate AppDomains is going to be a problem, especially if you want
>>>>> to execute the query in memory (you can hardly pass the entire object
>>>>> graph via remoting/serialization). Using Mono C# to compile to dynamic
>>>>> methods would be cool, but it'd be a lot of work, and would not work
>>>>> for queries that generate types (anonymous types or - implicitly - via
>>>>> transparent identifiers).
>>>>>
>>>>> VB10 was rumored to have REPL-support, so if you can wait for that and
>>>>> accept VB syntax, check out the RC. If that's not an option and you
>>>>> need unloading, I'd look at HQL again.
>>>>>
>>>>> Let us know how you solved that one! Support for user-provided LINQ is
>>>>> definitely an interesting feature.
>>>>>
>>>>> Stefan
>>>>>
>>>>> On 6 Mrz., 16:32, CassioT <[email protected]> wrote:
>>>>> > Because I already have the object graph and I don't want to hit the
>>>>> > database again.
>>>>> >
>>>>> > I used a DAO to get the first object only to give you a scenario, but
>>>>> > the real code is not like that.
>>>>> >
>>>>> > But even if it was the case I would chose LINQ. The string will be
>>>>> > written in the UI and I don't want HQL (or even SQL) in the UI. I
>>>>> want
>>>>> > it to be technology independent. That's why LINQ to NH is so waited.
>>>>> >
>>>>> > Thanks all replies.
>>>>> >
>>>>> > On Mar 5, 7:09 pm, Mohamed Meligy <[email protected]> wrote:
>>>>> >
>>>>> >
>>>>> >
>>>>> > > If you are using strings, why not use HQL directly? Then life
>>>>> should be
>>>>> > > pretty much more straight forward.
>>>>> > > Dynamic LINQ library (referred by Angel) is also great, and many
>>>>> tried it
>>>>> > > successfully.
>>>>> >
>>>>> > > --
>>>>> > > Mohamed Meligy
>>>>> > > Senior Developer, Team Lead Backup (.Net Technologies - TDG -
>>>>> Applications)
>>>>> > > Injazat Data Systems
>>>>> > > P.O. Box: 8230 Abu Dhabi, UAE.
>>>>> >
>>>>> > > Phone:  +971 2 6992700
>>>>> > > Direct:   +971 2 4045385
>>>>> > > Mobile:  +971 50 2623624, +971 55 2017 621
>>>>> >
>>>>> > > E-mail: [email protected]
>>>>> > > Weblog:http://gurustop.net
>>>>> >
>>>>> > > On Fri, Mar 5, 2010 at 9:53 PM, Angel Java Lopez <
>>>>> [email protected]>wrote:
>>>>> >
>>>>> > > > Hi people!
>>>>> >
>>>>> > > > Cassio, check the additional code described in
>>>>> >
>>>>> > > >
>>>>> http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1.
>>>>> ..
>>>>> >
>>>>> > > > I use some of this in my example
>>>>> > > >
>>>>> http://ajlopez.wordpress.com/2009/01/30/dynamic-expressions-example/
>>>>> > > > there are more links there, pointing to other implementation
>>>>> >
>>>>> > > > There is a Codeplex project, simplifyng the build of a
>>>>> QueryProvider, not
>>>>> > > > related with your question, but it could be interesting. I guess
>>>>> NHibernate
>>>>> > > > devs were using it to build the new Linq implemented in NH
>>>>> >
>>>>> > > > Angel "Java" Lopez
>>>>> > > >http://www.ajlopez.com
>>>>> > > >http://twitter.com/ajlopez
>>>>> >
>>>>> > > > On Fri, Mar 5, 2010 at 4:40 PM, Ken Egozi <[email protected]>
>>>>> wrote:
>>>>> >
>>>>> > > >> you could either look up c# parser and lexer codes, and tweak to
>>>>> your
>>>>> > > >> needs. Maybe look at Mono's c# compiler.
>>>>> > > >> that is however non trivial work.
>>>>> >
>>>>> > > >> you can also wrap the text with a valid c# class and method
>>>>> declarations,
>>>>> > > >> then use a CodeDomProvider.CompileAssemblyFromSource() to get an
>>>>> assembly,
>>>>> > > >> lookup your new type, and invoke the new method.
>>>>> >
>>>>> > > >> On Fri, Mar 5, 2010 at 9:23 PM, CassioT <[email protected]>
>>>>> wrote:
>>>>> >
>>>>> > > >>> Hi all. This is an NH off topic but it is C# and programming
>>>>> anyway.
>>>>> >
>>>>> > > >>> Let's suppose:
>>>>> >
>>>>> > > >>> var blog = dao.GetBlog(1); //Blog has posts
>>>>> >
>>>>> > > >>> string s = "blog.Posts.Sum(p => p.NumOfVisitors)";
>>>>> >
>>>>> > > >>> or
>>>>> >
>>>>> > > >>> string s = "Posts.Sum(p => p.NumOfVisitors)"; // considering
>>>>> blog as
>>>>> > > >>> the root object
>>>>> >
>>>>> > > >>> What is the best way to translate this string in code
>>>>> dynamically?
>>>>> > > >>> This is only a simple example but it is near of what I want.
>>>>> >
>>>>> > > >>> Thanks.
>>>>> >
>>>>> > > >>> --
>>>>> > > >>> You received this message because you are subscribed to the
>>>>> Google Groups
>>>>> > > >>> "nhusers" group.
>>>>> > > >>> To post to this group, send email to [email protected].
>>>>> > > >>> To unsubscribe from this group, send email to
>>>>> > > >>> [email protected]<nhusers%[email protected]>
>>>>> <nhusers%[email protected]<nhusers%[email protected]>>
>>>>> > > >>> .
>>>>> > > >>> For more options, visit this group at
>>>>> > > >>>http://groups.google.com/group/nhusers?hl=en.
>>>>> >
>>>>> > > >> --
>>>>> > > >> Ken Egozi.
>>>>> > > >>http://www.kenegozi.com/blog
>>>>> > > >>http://www.delver.com
>>>>> > > >>http://www.musicglue.com
>>>>> > > >>http://www.castleproject.org
>>>>> > > >>http://www.idcc.co.il-הכנס הקהילתי הראשון למפתחי דוטנט - בואו
>>>>> בהמוניכם
>>>>> >
>>>>> > > >> --
>>>>> > > >> You received this message because you are subscribed to the
>>>>> Google Groups
>>>>> > > >> "nhusers" group.
>>>>> > > >> To post to this group, send email to [email protected].
>>>>> > > >> To unsubscribe from this group, send email to
>>>>> > > >> [email protected]<nhusers%[email protected]>
>>>>> <nhusers%[email protected]<nhusers%[email protected]>>
>>>>> > > >> .
>>>>> > > >> For more options, visit this group at
>>>>> > > >>http://groups.google.com/group/nhusers?hl=en.
>>>>> >
>>>>> > > >  --
>>>>> > > > You received this message because you are subscribed to the
>>>>> Google Groups
>>>>> > > > "nhusers" group.
>>>>> > > > To post to this group, send email to [email protected].
>>>>> > > > To unsubscribe from this group, send email to
>>>>> > > > [email protected]<nhusers%[email protected]>
>>>>> <nhusers%[email protected]<nhusers%[email protected]>>
>>>>> > > > .
>>>>> > > > For more options, visit this group at
>>>>> > > >http://groups.google.com/group/nhusers?hl=en.- Zitierten Text
>>>>> ausblenden -
>>>>> >
>>>>> > - Zitierten Text anzeigen -
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "nhusers" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]<nhusers%[email protected]>
>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/nhusers?hl=en.
>>>>>
>>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nhusers" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<nhusers%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/nhusers?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "nhusers" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<nhusers%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/nhusers?hl=en.
>>>
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to