Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a linq expression dynamically or parsing the string value of the expression
    primarykey
    data
    text
    <p>Got an problem where I need to crate an linq expression dynamically. But I cant seem to wrap my head around it. </p> <p>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: </p> <pre><code>var result = from n in data where n.Item1 == "someValueFromCheckBox" select n; </code></pre> <p>Where the data, is an tupled list like : </p> <pre><code>List&lt;Tuple&lt;string, string&gt;&gt; </code></pre> <p>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: </p> <pre><code>var result = from n in data where n.Item1 == "someValueFromCheckBox" || n.Item1 == "someValueFromCheckBox1" select n; </code></pre> <p>So my first thought was to build the linq statement as a string. In the following manner: </p> <pre><code> var selectedItems = checkedListBoxSelectedTerms.CheckedItems; var linqStatement = ""; for (int i = 0; i &lt; 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); </code></pre> <p>But that did not work as planned, because when I put it in like this: </p> <pre><code>var result = from n in data where n.Item1 == linqStatement select n; </code></pre> <p>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. </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload