How to use predicate builder using linq2sql and OR

I have two tables (TABLE1, TABLE2 - unique I know), which has a 1-to-many relationship and a foreign key between the ID columns of both tables.

Using linq2sql I am trying to select all the TABLE1 entries so that their corresponding TABLE2 values contain at least 1 element in the list that I go through it.

Here is an example of the code that I used in LINQPad (awesome program) to test it, but I get a NotSupportedException error : Unsupported overload used for the request operator "Any".

long[] items = { 3, 5, 8 };
var predicate = PredicateBuilder.False<TABLE2>();

foreach (long i in items)
{
    long t = i;
    predicate = predicate.Or(att => att.ID == t);
}

//TABLE2.Where(predicate).Dump(); //works like a charm

IQueryable query = 
    from t1 in TABLE1
    where t1.TABLE2.AsQueryable().Any(predicate) //problem with this line
    select a;

query.Dump();

UPDATE

LinqKit LinqPad LinqKit.dll, Include PredicateBuilder, LinqKit " ".

+2
1

  • AsExpandable() TABLE1
  • () , EntitySet.

,

IQueryable query = 
    from t1 in TABLE1.AsExpandable()
    where t1.TABLE2.Any(predicate.Compile()) //the problem should disappear
    select a;

.

+2

Source: https://habr.com/ru/post/1687365/


All Articles