how to improve nested linq query
-
I would like to be able to call string tag1 & tag2 with multiple values, such as
Api/test?tag1=123,456,789&tag2=987,654
However, the below contain function only allows me to call 45 values for each tag1 & tag2, anything more than 45 string values used to search tag1 & tag2, the code throws a nested query error:"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.
I tried adding toList() to my tag1 method, but I keep getting syntax error:Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)
any further advice, would be very much appreciated. Thank you.IQueryable Data = null;
if (!string.IsNullOrEmpty(query.tag2)) { var ids = query.tag2.Split(','); var dataMatchingTags = db.database\_BCs.Where(c => ids.Contains(c.TAG2)); if (Data == null) Data = dataMatchingTags; else Data = Data.Union(dataMatchingTags); } if (!string.IsNullOrEmpty(query.tag1)) { var ids = query.tag1.Split(','); var dataMatchingTags = db.database\_BCs.Where(c => ids.Contains(c.TAG1)); if (Data == null) Data = dataMatchingTags; else **Data = Data.Union(dataMatchingTags).ToList();** } if (Data == null) // If no tags is being queried, apply filters to the whole set of products Data = db.database\_BCs; if (query.dl\_type != null) { Data = Data.Where(c => c.Type == query.dl\_type); } **var data = Data.ToList();** if (!data.Any()) { var message = string.Format("No data found"); return Request.CreateErrorResponse(HttpStatusCode.NotFound, message); }