在动态linq查询中使用变量

时间:2012-05-01 11:21:55

标签: c# linq dynamic-linq

我正在使用Linq to Entities并添加了使用using System.Linq.Dynamic;的stmt 我的目标是将whereClause变量传递到emailList查询中(参见屏幕截图)。

有什么想法?

Error message details

+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++
在使用@Michael建议后,我得到了以下内容:

HTML(注意我将字段值放在'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>

代码背后:

// 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 个答案:

答案 0 :(得分:2)

您需要将与参数匹配的对象传递给IQueryable.Where( predicate )

所以whereClause必须是这种类型的对象:

Expression<Func<TSource, bool>>

因为你正在对你的where子句进行ORing而不是它们,你必须构建一个大的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's Predicate Builder

答案 1 :(得分:1)

您应该可以使用Linq Dynamic Query来执行此操作:

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

这就是图书馆的设计目标。

答案 2 :(得分:0)

您使用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");

使用参数:

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

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

<强>更新

不要使用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

实时测试:http://ideone.com/EFhnA