How to implement tsql equivalent "IN" using dynamic Linq

I am creating a generic form (C #) that can receive any Linq request. In this form, I want to be able to add filters (WHERE clauses). For operators like '=', '>', 'Like', etc., I can do things like IQueryable.Where(someFieldname + "> @0", someCriteria). But when I want to be able to make the equivalent of T-sql "IN", I am completely lost. I searched for a watch, but cannot find a way to figure it out.

When you think about how this is possible, you need to put the values ​​for the IN clause in a string array or some other simple list of strings. Then attach this list to the base query. But how can I join these two when the base request can be any request?

Example: Let's say my basic query looks something like this:

IQueryable<Object> q = from a in db.Adresses
                       select new { a.Street, a.HouseNr };

In the form I want to be able to filter on HouseNr as follows: Where HouseNr IN (1, 3, 5) Numbers (1, 3, 5) are available in an array of strings (or any other list of strings).

How can I achieve this, knowing that a basic query can be anything?

+5
source share
4 answers
int[] list = new[]{1, 3, 5};
IQueryable<Object> q = from a in db.Adresses
                       where list.Contains(a.HouseNr)
                       select new { a.Street, a.HouseNr };
+4
source

Containsdisplayed in INSQL generation.

int[] numbers = new int[] { 1, 3, 5 };

var q = from a in db.Addresses
        where numbers.Contains(a.HouseNr)
        select new { a.Street, a.HouseNr }
+2
source

How about this ...

string[] Numbers = new string[]{"1", "3", "5"};
IQueryable<Object> q = db.Adresses.Where(a => Numbers.Contains(h.HouseNr));
0
source

Just a thought ... isn't the method returning the .Intersect()values ​​of a list in which the values ​​of both values ​​exist?

0
source

All Articles