2

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.

Thank you anyway. Have a nice day.

David Gourde
  • 3,709
  • 2
  • 31
  • 65
  • 1
    Definitely [XY problem](http://meta.stackexchange.com/a/66378). Use `PredicateBuilder` instead to get multiple `OR` conditions. – MarcinJuraszek May 14 '15 at 21:44
  • There is many ways to do "OR"s depending on what type of OR you need to do via `Aggregate`, `Intersects`, `Contains`, and `Any`. – Robert McKee May 14 '15 at 22:21

3 Answers3

1

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.

David Gourde
  • 3,709
  • 2
  • 31
  • 65
  • 1
    I'm in the same situation. What I have done so far is use a LINQ query which returns no rows, and for some reason, that works fine. New lists (like Robert's answer) and `Enumerable.Empty<>` don't work. – Andrew Mar 23 '16 at 18:47
0

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.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • I already use a "Contains" to query my database to test if x is in it. I would have to test for all x in a list of x, and for each of them I would have to test the contains. I have no idea how to do this with LINQ. – David Gourde May 15 '15 at 23:49
  • If you give some example data, inputs, and wanted output, I'm sure we could help you. – Robert McKee May 17 '15 at 00:06
0

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.

Community
  • 1
  • 1
Andrew
  • 7,602
  • 2
  • 34
  • 42