locked
Web API returns the wrong amount of data RRS feed

  • Question

  • User1121128150 posted

    I have a very very simple webapi that returns the wrong amount of data when run, its so simple I am totally buffed as to how this can happen.

    The API controller looks like below

    [HttpGet("{TillID}")]
            public IEnumerable<object> Get(int tillID, int StartAtRow=0, int TakeNoOfRows=1000)
            {
                string hmCompanyId;
                string languageName;
                PrepareParameter(tillID, out hmCompanyId, out languageName);
                
                var tillData = from tbd in _context.TillBasicData.OrderBy(i => i.ItemId).Skip(StartAtRow).Take(TakeNoOfRows)
                               where tbd.CompanyId == hmCompanyId && tbd.languageCode == languageName
                               select tbd;
    
                return tillData.ToList();
            }

    if I invoke that API using a browser with the following URL

    https://XXX.YYYY.ZZZ:5443/api/till/tilldata/1?StartAtRow=0&TakeNoOfRows=10

    I would expect to get 10 rows back, but I get only 3?

    if I invoke the API using a browser with the following URL

    https://XXX.YYY.ZZZ:5443/api/till/tilldata/1?StartAtRow=0&TakeNoOfRows=20

    I get 5 returned rows

    How is it possible that such a simple API is giving the wrong amount of data? Manual SQL query directly in the DB confirms the data is correct

    What am I missing here? I don't think I can make the API any simpler 

    Friday, June 2, 2017 1:51 PM

Answers

  • User1967761114 posted

    Hi mattdouhan @outlook.com,

    According to your description, and as zxj said, I suggest you could use where in lambda expression before order.

    So your code could modify like this:

     var tillData = from tbd in _context.TillBasicData.Where(m => m.CompanyId == hmCompanyId && m.languageCode == languageName).OrderBy(i => i.ItemId).Skip(StartAtRow).Take(TakeNoOfRows)
                    select tbd;
    

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 7, 2017 9:28 AM

All replies

  • User991499041 posted

    Hi mattdouhan,

    How is it possible that such a simple API is giving the wrong amount of data? Manual SQL query directly in the DB confirms the data is correct

    What am I missing here? I don't think I can make the API any simpler 

    Based on the code, the where clause is used in the query expression to specify which elements from the data source will be returned in the query expression. I guess that's probably why you get the wrong amount of data.

    var tillData = from tbd in _context.TillBasicData.OrderBy(i => i.ItemId).Skip(StartAtRow).Take(TakeNoOfRows)
                   //where tbd.CompanyId == hmCompanyId && tbd.languageCode == languageName
                   select tbd;

    You could comment out where clause and check the result.

    The Visual Studio debugger helps you observe the run-time behavior of your program and find problems.

    Navigating through Code with the Debugger
    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx 

    Regards,

    zxj

    Monday, June 5, 2017 6:15 AM
  • User1967761114 posted

    Hi mattdouhan @outlook.com,

    According to your description, and as zxj said, I suggest you could use where in lambda expression before order.

    So your code could modify like this:

     var tillData = from tbd in _context.TillBasicData.Where(m => m.CompanyId == hmCompanyId && m.languageCode == languageName).OrderBy(i => i.ItemId).Skip(StartAtRow).Take(TakeNoOfRows)
                    select tbd;
    

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 7, 2017 9:28 AM