3

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

有什么想法吗?

错误消息详细信息

++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
使用@Michael建议后,我得到了它与以下内容一起使用:

HTML(注意我将字段值放在“值”属性中。):

<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;
4

3 回答 3

2

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

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

Expression<Func<TSource, bool>>

因为你是在 ORing 你的 where 子句而不是 ANDing 它们,你将不得不构建一个大的 where 子句。

假设您的数据对象是 OBJ 类型并且它具有布尔属性 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

于 2012-05-01T11:32:39.597 回答
1

您应该可以使用 Linq 动态查询来做到这一点:

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

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

于 2012-05-01T11:31:32.177 回答
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

于 2012-05-01T11:32:23.317 回答