in a dynamic linq query utilizing variables

c# dynamic-linq linq

Question

I am using Linq to Entities and have added the using stmt of using System.Linq.Dynamic; My goal is to pass in the whereClause variable into the emailList query (see screen shot).

Any thoughts?

Error message details

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
After using @Michael suggestion I got it to work with the following:

HTML (notice I placed the field value in 'Value' attr.):

<asp:CheckBoxList ID="_checkboxGroups" runat="Server">
                            <asp:ListItem Text="Sid Dickens Lovers" Value="SidDickens_TF" Selected="False" />
                            <asp:ListItem Text="Rosamond Lover" Value="Rosamond_TF" Selected="false" />
                            <asp:ListItem Text="Wine and Cheese Lovers" Value="WineAndCheese_TF" Selected="false" />
                            <asp:ListItem Text="Good Clients" Value="IntDesign_TF" Selected="false" />
                            <asp:ListItem Text="Vendors" Value="Vendor_TF" Selected="false" />
                        </asp:CheckBoxList>

Code behind:

// determine # of items in asp:CheckBoxList
        var groupCount = _checkboxGroups.Items.Count;

        var conditions = new List<string>();

        for (int i = 0; i < groupCount; i++)
        {
            if (_checkboxGroups.Items[i].Selected)
            {
                conditions.Add(_checkboxGroups.Items[i].Value.ToString() + " == true");
            }
        }

        string whereClause = string.Join(" OR ", conditions.ToArray());


        ElanEntities3 db = new ElanEntities3();

        var emailList = (from c in db.vEmailListViewforSendings
                         orderby c.Email
                         select c).AsQueryable();

        emailList = emailList.Where(whereClause);

       _listViewClients.DataSource = emailList;
1
3
5/4/2012 12:00:00 PM

Accepted Answer

You use System.Linq.Dynamic http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

var email = from c in db.MailListViewForSendings
            order by c.ID
            select c;

// then you chain the Linq;
email = email.Where("CategoryID=3");

To use parameters:

var email = from c in db.MailListViewForSendings
            order by c.ID
            select c;

// then you chain the Linq;
email = email.Where("CategoryID=@0", 3);

UPDATE

Don't use StringBuilder, use List<string> instead, then concatenate it by string.Join:

using System;

using System.Collections.Generic;

public class Test
{
        public static void Main()
        {
             var conditions = new List<string>();
             conditions.Add("Lastname = 'Lennon'");
             conditions.Add("Firstname = 'John'");
             conditions.Add("Age = 40");

             Console.WriteLine(string.Join(" OR ", conditions.ToArray() ));
        }
}

Output:

Lastname = 'Lennon' OR Firstname = 'John' OR Age = 40

Live test: http://ideone.com/EFhnA

0
5/1/2012 1:41:08 PM

Popular Answer

You need to pass an object that matches the parameter to IQueryable.Where( predicate )

So whereClause must be an object of this type:

Expression<Func<TSource, bool>>

Because you are ORing your where clauses not ANDing them, you will have to build one big where clause.

Assuming your data object is of type OBJ and it has bool properties P0 and P1:

bool filterP0 = _checkboxGroups[0].Selected;
bool filterP1 = _checkboxGroups[1].Selected;

Expression<Func<OBJ, bool>> predicate = o =>
(
    ( !filterP0 && !filterP1 )
    ||
    ( filterP0 && o.P0 )
    ||
    ( filterP1 && o.P1 )
);

var emailList =
    db.vEmailListViewForSendings
        .Where( predicate )
        .OrderBy( o => o.ID );

That's the gist anyway.


Or, if you really must build the predicate dynamically, you could use Joe Albahari's Predicate Builder.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow