locked
union linq query - responding with no data RRS feed

  • Question

  • I am trying to execute the following get request (api/test?tag1=123), but the query returns me with no data response, as an output. 

    the above request works, if i remove the following line of code:

    Data = Data.Where(c => (c.Cover == "TK" && c.UploadDate <= fourth) || 
    (c.Cover == "TD" || c.Cover == "CV" && c.UploadDate <= yester));

    I would like to seek hep, as to how can I get my tag1 parameters to output data with the above where clause.  

    There is data for tag1=123, under the above where clause.  

    				IQueryable<database_BCs> 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);
                    }
    
                    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);
                    }
    
                    if (query.endDate != null)
                    {
                        Data = Data.Where(c => c.UploadDate <= query.endDate);
                    }
    
                    if (query.startDate != null)
                    {
                        Data = Data.Where(c => c.UploadDate >= query.startDate);
                    }
                    DateTime fourth = DateTime.Today.AddBusinessDays(-4);
                    DateTime yester = DateTime.Today.AddBusinessDays(-1);
    
                    Data = Data.Where(c => (c.Cover == "TK" && c.UploadDate <= fourth) || (c.Cover == "TD" || c.Cover == "CV" && c.UploadDate <= yester));
                    Data = Data.OrderByDescending(c => c.UploadDate);
    Thank you very much for your help and time.

    Monday, February 9, 2015 12:30 PM

Answers

  • Hello missy786,

    >> There is data for tag1=123, under the above where clause. 

    Is that you mean after running your commented code, you could still get the result as if you use 

    Data = Data.OrderByDescending(c => c.UploadDate).Tolist();

    However, the data is just not shown in the in your request client?

    If so, I suggest you could comment some if statement to see if it is caused by the union syntax, e.g, you could comment out

    if (!string.IsNullOrEmpty(query.tag2))
    
    …
    
    if (!string.IsNullOrEmpty(query.tag1))
    
    …
    

    If not, my suggestion is that you could split the complex where clauses to multi where clause as:

    Data = Data.Where(c => (c.Cover == "TK" && c.UploadDate <= fourth));
    
    Data = Data.Where(c => (c.Cover == "TD" || c.Cover == "CV" && c.UploadDate <= yester));
    

    Adding a breakpoint at this first line, and when you debug the query, please check which line would return a no record result and check the conditions carefully to see why it does not meet the requirement as the c.Cover is TT, then no records would return.

    By the way, it is not very clear that what project you are using, for Rest project, if WebAPI, I suggest that you could post it to:

    http://forums.asp.net/1246.aspx/1?Web+API

    if WCF Data Service, here:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetdataservices

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Wednesday, February 18, 2015 7:06 AM
    Tuesday, February 10, 2015 8:49 AM