Dynamic LINQ string expressions

Posts   
 
    
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 09-Dec-2009 10:17:33   

Hello,

I've found the following LINQ example at Scottgu's blog (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx):


Dim Northwind As New NorthwindDataContext

Dim query = Northwind.Products _
        .Where("CategoryId=2 AND UnitPrice>3") _
        .OrderBy("SupplierId")

Gridview1.Datasource = query
Gridview1.DataBind()

Now I've got the following query:


var q = (
                        from p in MetaData.VwInternetArtikelenTocelo
                        select p
                    );

After this I want to add an extra where clause like this:


q.Where("FoodProducts=1");

This gives me the next exception:

Error 3 The type arguments for method 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. C:\SVN\Tocelo\Tocelo.UmbracoExtensions\Tocelo.BusinessLogic\BLL\Controllers\ProductsController.cs 73 13 Tocelo.BusinessLogic

It looks like I can only add a predicate, but I want to add an string expression. Is this possible using Linq to LLBLGen Pro?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Dec-2009 10:31:05   

It looks like I can only add a predicate, but I want to add an string expression. Is this possible using Linq to LLBLGen Pro?

May I ask why you want to add a string expression instead of a predicate.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 09-Dec-2009 10:35:18   

Walaa wrote:

May I ask why you want to add a string expression instead of a predicate.

Because I'm building the string dynamically and it could be different every time it is called. For example it could be:

q.Where("FoodProducts=1");

But it could also be:

q.Where("ElectricProducts=1");
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Dec-2009 11:23:52   
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 09-Dec-2009 13:27:44   

Managed to get it fixed myself. I thought string expressions where a standard part of LINQ, but I still needed to add the DynamicLibrary.cs file to my class library and include System.Linq.Dynamic.

Now it works simple_smile .

Posts: 1
Joined: 06-Feb-2010
# Posted on: 06-Feb-2010 18:18:33   

jbreuer would you mind posting how you fixed the problem yourself and added a string expression because I can't figure out how to do it. Thanks.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 08-Feb-2010 09:08:57   

BruceHendrix wrote:

jbreuer would you mind posting how you fixed the problem yourself and added a string expression because I can't figure out how to do it. Thanks.

Bruce Hendrix

I did the following steps:

  1. Downloaded the Dynamic Linq code at http://www.scottgu.com/blogposts/dynquery/dynamiclinqcsharp.zip

  2. Added the code to my project (not as a bin, but just copied the code)

  3. Added the following namespace to the class which needs the dynamic query: using System.Linq.Dynamic;

After this you can use dynamic queries like the following example:


public VwInternetArtikelenToceloCollection GetProducts(string letter, ProductGroup productGroup)
        {
            var q = (
                        MetaData.VwInternetArtikelenTocelo
                            .Where(string.Concat(productGroup, "=1"))
                            .Where(p => p.Description.ToUpper().StartsWith(letter))
                            .OrderBy("Description")
                    );

            return ((ILLBLGenProQuery)q).Execute<VwInternetArtikelenToceloCollection>();
        }

In this example ProductGroup is an enum so that's why string.Concat works.

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 08-Feb-2010 20:42:40   

I guess my question would be why would anyone want to do that? The benefit of a statically typed language in this case is that everything is statically checked at compile time. In this example, the entities, properties, or whatever else you put in the string is not checked at compile time. I personally think the Predicate Builder is a better way to go for this reason.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 09-Feb-2010 09:08:31   

Seth wrote:

I personally think the Predicate Builder is a better way to go for this reason.

Could you give an example how the code above would be written using the predicate builder? I'm not really good at using the predicate builder, but an example might help.

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 09-Feb-2010 17:59:50   

Have you seen this?

mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 18-Feb-2010 01:22:23   

I just thinking out loud I think string expression is a solution for dynamic expression and dynamic operator

for example: If I need to implement wildcard search, so need to choose one of (==, Contains, StartWith or EndWith) depends on text analysis for input parameter.

thus, need string expression

any one agree with me...

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 18-Feb-2010 21:32:09   

As with all things, there is no one correct solution, it depends on what you are trying to achieve simple_smile

Matt