动态创建linq表达式或解析表达式的字符串值 [英] Creating a linq expression dynamically or parsing the string value of the expression

查看:80
本文介绍了动态创建linq表达式或解析表达式的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到了一个需要动态创建linq表达式的问题.但是我似乎无法绕过它.

Got an problem where I need to crate an linq expression dynamically. But I cant seem to wrap my head around it.

因此,首先我得到一个复选框.我从所选文件中输入值的位置,因此在运行代码之前,我将不知道会有多少个参数.然后计划是基于复选框中元素的名称构造一个linq句子.让我说明一下: 我想创建一个如下所示的linq语句:

So first off I got an check list box. Where I input values from a file that is selected, hence I will not know how many parameters there will be there before I run the code. Then the plan is to construct a linq sentence based on the name of the elements in the checkbox. Let me illustrate: I want to create an linq statement that looks like this:

var result = from n in data where n.Item1 == "someValueFromCheckBox"  select n;

数据所在的是一个元组列表,例如:

Where the data, is an tupled list like :

List<Tuple<string, string>> 

这很好,只要用户仅从复选框中选择一项即可.但是,例如当用户选择两个项目时,我需要创建一个如下所示的linqu表达式:

And that is fine as long as the user only selects one item from the checkbox. But when the user for example selects two items, I need to create a linqu expression that looks like this:

var result = from n in data where n.Item1 == "someValueFromCheckBox" || n.Item1 == "someValueFromCheckBox1" select n;

所以我的第一个想法是将linq语句构建为字符串.以以下方式:

So my first thought was to build the linq statement as a string. In the following manner:

        var selectedItems = checkedListBoxSelectedTerms.CheckedItems;
        var linqStatement = "";
        for (int i = 0; i < selectedItems.Count; i++ )
        {          
                linqStatement += selectedItems[i].ToString() + "|| n.item1 ==" ;                                  
        }
        //this is simply to remove the || "n.Item1 ==", at the end because it 
        is not needed after the last selecteditem. 
        linqStatement = linqStatement.Remove(linqStatement.Length - 13, 13);

但是这并没有按计划进行,因为当我这样输入时:

But that did not work as planned, because when I put it in like this:

var result = from n in data where n.Item1 == linqStatement  select n;

整个事情变成一个字符串.而且我不能将"n.Item1 =="作为字符串.从理论上讲,该解决方案非常适合我使用,因为我不必担心复选框中元素的复杂程度以及用户选择了多少元素,但是我不能将linqStatement整体作为字符串传递.因此,可以寻求任何帮助,因为我似乎找不到一个很好的例子来做这样的事情.

The whole thing becomes a string. And I cant have the "n.Item1 ==" to be a string. In theory that solution would work great for my use, because I would not have to worry about how manay elements that are in the checkbox and how many elements the user selects, but I cannot pass linqStatement whole as a string. So any help would be appriciated, because I cant seem to find an good example to do something like this.

提前谢谢!

推荐答案

对于这样一个简单的示例,您应该只具有

For such a simple example, you should simply have a list or set of strings, and then do Contains. E.g.

var selectedItems = checkedListBoxSelectedTerms.CheckedItems
                        .Select(x => x.ToString()).ToList();
var result = from n in data where selectedItems.Contains(n.Item1) select n;

如果您需要更灵活的动态构建功能,请尝试使用

If you need more flexible dynamic building capabilities, try something like Dynamic LINQ or PredicateBuilder.

这篇关于动态创建linq表达式或解析表达式的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆