David Gourde
David Gourde

Reputation: 3914

Concatenate IQueryable starting with Empty IQueryable in VB

I am trying to concatenate multiple LINQ requests returns (all the returns are IQueryable).

My problem is that I need to start with an Empty IQueryable variable, but the ".Concat" method doesn't work with a variable that is "Nothing".

I was wondering if you had a way of doing this?

PS: You can use multiple "Where" requests as "AND" but not as "OR" (Seq.Where(Seq.Where())). I am trying to do the latter by concatenating multiple requests. Also, if I do an impossible request (A request returning no match), it works, but it is clearly not a good idea.

Upvotes: 2

Views: 775

Answers (3)

Andrew
Andrew

Reputation: 7880

I am struggling with the same problem. I found two options that could help you but have a drawback.

Instead of using AsQueryable() (suggested in other posts), try these (sorry, just C# code, but I guess it may be useful anyway):

var query = new List<ServiceTicketCombinedHistory>().Take(0);
var query = Enumerable.Empty<ServiceTicketCombinedHistory>();
// Optionally:
var query = Enumerable.Empty<ServiceTicketCombinedHistory>().Take(0);

The problem in my case is that my query has a Where(x => x.Field.Contains(value)), and with these options, this comparison seems to be done on memory and is case sensitive. By using an empty LINQ query result (like you did), the comparison seems to be done in the database with a LIKE and is case insensitive. More info about this difference here.

The LINQ query approach I used, instead of deleting the item, is to add a Take(0) to the whole query (you have to surround it with parenthesis). That results in the simplest execution plan for the SQL Server.

Upvotes: 0

David Gourde
David Gourde

Reputation: 3914

Finally I had to do my request once on the first item of the list, then remove the item. After that, I could simply do a for each (using "Contains" to query my database).

Still, this is not really an answer I think. It pretty much looks like a hack.

I did not find a way to to my query with linq.

I was querying my database using Contains and verifying if x was in the database. But I had to to this for a list of x.

Upvotes: 1

Robert McKee
Robert McKee

Reputation: 21477

Try:

var start=(new List<something>()).AsQueryable();

Although this will likely only work if your IQueryable is over the same source (in this case, objects). I suggest trying to figure out a LINQ way of doing your "OR" properly, either through Aggregate, Intersects, Contains, Any or PredicateBuilder.

Upvotes: 0

Related Questions