Dynamic Linq 'contains' clause without using placeholder

asp.net asp.net-core-mvc c# dynamic-linq linq

Question

The syntax given for contains clause is

ids = new int[] {1,2,3,4};
dataContext.Table.Where("@0.Contains(id)", ids);

But what I want is

dataContext.Table.Where("{1,2,3,4}.Contains(id)"); //getting exception here

[ERROR] Expression expected (at index 0)

I need this because the where clause my or may not use the contains method. it depends on how user acts.

1
-1
6/1/2017 3:54:25 AM

Accepted Answer

so I got the answer for this after tinkering for sometime. So posting the answer here.

 dataContext.Table.Where("new Int[]{1,2,3,4}.Contains(id)");

You can use whatever datatype you need. I use reflection to find datatype and use that accordingly.

0
6/1/2017 5:13:11 AM

Popular Answer

try code:

int[] ids= {1,2,3,4};

dataContext.Table.Where(c=>c.ids.Contains(t.id)).ToList();

Or

var result= (from p in dataContext.Table.AsEnumerable()
                      join q in ids on p.id equals q
                        select p).Distinct() .ToList();


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