Reputation: 6481
See the following code:
var source = Peoples.AsQueryable();
IQueryable<People> p;
p = source.Where( x => false);
p = p.Concat(source.Where( x => x.FirstName == "DAVID"));
p = p.Concat(source.Where(x => x.City_id == 3));
p.Dump(); //I use linqpad but it does not matter now
this code work, and linq2sql product one sql-statement (with three qouery and UNION ALL
).
now the same code, ostensibly exactly the same procedure, in another variation, throw NotSupportedException:
var q = new IQueryableUnderstand<People>(Peoples.AsQueryable());
q.AddToList(x => x.City_id == 3);
q.AddToList(x => x.FirstName == "Dave");
q.Results().Dump();
IQueryableUnderstand Class:
class IQueryableUnderstand<T>
{
IQueryable<T> _source;
IQueryable<T> combin;
public IQueryableUnderstand(IQueryable<T> source)
{
_source = source;
combin = _source.Where(s => false);
}
public void AddToList(Func<T, bool> predicate) => combin = combin.Concat(_source.Where(predicate));
public IQueryable<T> Results() => combin;
}
LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator
I feel stupid, what's the difference?
Upvotes: 1
Views: 61
Reputation: 134571
Your AddToList()
method accepts a Func<T, bool>
predicate. The parameter type is incorrect. If applied to your query, it will convert it to a Linq-To-Objects query. IQueryable<TSource>
expects expressions, not delegates.
Change your method signature:
public void AddToList(Expression<Func<T, bool>> predicate) =>
combin = combin.Concat(_source.Where(predicate));
Upvotes: 1