Using variables in linq dynamic query

I use Linq for Entities and added the use of stmt. using System.Linq.Dynamic; My goal is to pass the variable whereClauseto the emailList request (see screenshot).

Any thoughts?

Error message details

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

HTML (notice I put the field value in the "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;
+3
source share
3 answers

You are using 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 the options:

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

StringBuilder, List<string>, 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() ));
        }
}

:

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

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

0

, , IQueryable.Where()

, whereClause :

Expression<Func<TSource, bool>>

ORing , ANDing, where.

, OBJ bool P0 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 );

, .


, , Joe Albahari Predicate Builder.

+2

All Articles