3

私は Linq to Entities を使用してusing System.Linq.Dynamic; おり、変数を emailList クエリに渡すことが目標の using stmt を追加しましたwhereClause(スクリーン ショットを参照)。

何かご意見は?

エラーメッセージの詳細

++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
@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>>

where 句を AND ではなく OR しているため、1 つの大きな 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を使用できます。

于 2012-05-01T11:32:39.597 に答える
1

Linq Dynamic Query を使用して実行できるはずです。

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 に答える