Using Linq DynamicQuerable for a Not IN sql type operation

c# dynamic-linq linq

Question

I am using the Linq DynamicQuerable code. I have an array of integers and a string representing the field I want to use as the excluding filter.

For example

IQuerable GetItemsWithoutExcluded(IQuerable qry, string primaryKey, List<int> excludedItems) {
        // psuedo code
        return qry.Where("p=>! p.primaryKey in excludedItems");
    }

Any idea how I would accomplish this using DynamicQuerable?

1
0
8/17/2012 1:24:22 PM

Popular Answer

You can do this without dynamic-LINQ. Dynamic-LINQ is not a full fledged citizen of the framework. And by changing the way you accomplish this task you can use a more effective filter. The way you have it now the method needs to be called for each exclusion.

Try something like this instead

        List<int> allKeys = new List<int>{1,2,3,4,5,6};            
        List<int> excluded = new List<int>{ 1, 2, 3 };

        var query = from included in allKeys
                    where !excluded.Contains(included)
                    select included;

        foreach (var item in query)
            Console.WriteLine(item);
2
8/17/2012 1:33:10 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow