Null linq query issue - web api
-
Dear all, I am having a little issue with calling couple of queries from my filter method and would like to seek some a guidance to what I may doing wrong. if I call (api/test?tag=##,##,...), the query works fine. However, if I do not use the line of code --
var data2 = db.data_qy.AsQueryable();
and I call ((api/test?price_type=BVR), the query works and result is shown, otherwise if I do not use that line of code, i get a error -- ExceptionMessage":"Value cannot be null Also, If I usevar data2 = db.data_qy.AsQueryable();
and call query ((api/test?price_type=BVR&deal_type=TYP), the query outputs entire dataset, instead filtering result to the search query. Please help. Many thankspublic HttpResponseMessage get([FromUri] Query query)
{
IQueryable data = null;if (!string.IsNullOrEmpty(query.tag)) { var ids = query.tag.Split(','); var dataMatchingTags = db.data\_qy.Where(c => ids.Any(id => c.TAG.Contains(id))); if (data == null) data = dataMatchingTags; else data = data.Union(dataMatchingTags); } var data2 = db.data\_qy.AsQueryable(); if (query.deal\_type != null) { data = data2.Where(c => c.Type == query.deal\_type); } if (query.price\_type != null) { data = data2.Where(c => c.Cover == query.price\_type); } var materializedData = data.ToList(); if (!materializedData.Any()) { var message = string.Format("No data found"); return Request.CreateErrorResponse(HttpStatusCode.NotFound, message); } return Request.CreateResponse(HttpStatusCode.OK, materializedData); }