【问题标题】:Filter bind source过滤绑定源
【发布时间】:2015-09-01 00:42:21
【问题描述】:

我有一个包含以下数据的绑定源

TOM JACK
JACK TOM
TOM DEISE
JACK JENNY
TOM DALTON
JERRY JOY
JOY JERRY

我有一个组合框,里面也填充了上述数据,例如如果我选择JACK TOM,那么绑定源应该过滤结果,以便我需要得到以下结果

结果:

TOM JACK
JACK TOM

(只想显示同时包含 WORD('TOM' 和 'JACK')的名称)

以下是我的尝试

bndSourceGrid.Filter = String.Format("{0} LIKE '%{1}%'", "Name", cboName.Text)
bndSourceGrid.Sort = "Name ASC"

【问题讨论】:

    标签: c# regex vb.net


    【解决方案1】:

    您需要将查询构建成如下所示:

    Name LIKE '%tom%' AND Name LIKE '%jack%' ....
    

    因此,接受您的输入,将其拆分,将其投影到一个新字符串中,然后使用 AND 将它们全部连接在一起:

    bndSourceGrid.Filter = 
        string.Join(" AND ",
            cboName.Text
            .Split(' ')
            .Select(s => string.Format("Name LIKE '%{0}%'", s))
        );
    

    如 cmets 中所述,您可能需要将 Select 的结果作为数组返回:

    bndSourceGrid.Filter = 
        string.Join(" AND ",
            cboName.Text
            .Split(' ')
            .Select(s => string.Format("Name LIKE '%{0}%'", s))
            .ToArray()
        );
    

    【讨论】:

    • 收到此错误Unable to cast object of type 'WhereSelectArrayIterator2[System.String,System.String]' to type 'System.String[]'.
    • 仅供参考,我将代码转换为 vb.net 以在我的环境中工作,即bndSourceGrid.Filter = String.Join(" AND ", cboName.Text.Split(" "C).[Select](Function(s) String.Format("Name LIKE '%{0}%'", s)))
    • 那你为什么不用VB.Net 而不是C# 来标记你的问题呢?我不知道 VB.Net 如何使用 Linq。不知道为什么会出现错误,也许您可​​以在Select 之后附加.ToArray()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多