Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Null linq query issue - web api

Null linq query issue - web api

Scheduled Pinned Locked Moved ASP.NET
helpdatabaselinqcsharpjson
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    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 use var 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 thanks

    public 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);
        }
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups