Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to display the number of count but I am currently not getting anything on the client end(api/co?name=card). I would like to be able to show count as "10 records displayed of 10 found" This is what I have so far:

C#
public HttpResponseMessage Get([FromUri] Query query)
        {
            var data = db.database.AsQueryable();

            //Page Index (0-based to indicate which page you are on)
            int page = 0;

            //Page Size (how many results to display per page)
            int pageSize = 10;
            //So if you wanted to grab the first page of elements, you would use the following for your data

            if (query.name != null)
            {
                data = data.Where(c => c.Name == query.name);
            }
            if (query.price != null)
            {
                data = data.Where(c => c.Price== query.price);
            }
            if (!data.Any())
            {
                var message = string.Format("No data was found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }
            
            int SelectedRows = data.Count();
            return Request.CreateResponse(HttpStatusCode.OK, data);
            
        }


Any advice or guide I should be looking into get this issue solved.
Many thanks.
Posted
Updated 3-Feb-14 23:30pm
v2
Comments
JoCodes 31-Jan-14 11:06am    
Use else if and else instead of multiple if statements so that it wont enter to the conditions again and change the return data. Then take the count
miss786 31-Jan-14 11:19am    
Thank you so much for your response. I changed the conditions into else if but I am still not getting any results. If you do not mind clarifying, what did you mean by the following statement "change the return data". Many thanks for your help.
JoCodes 31-Jan-14 11:27am    
Sure, the if statement block before was having chances to enter multiple times that means once it enters into one if condition and return some values to "data". again if it enters the below if statement then the values will be again modified . but in if else statement it wont happen . But if it was intentional then can use if () with AND , OR operator too.
JoCodes 31-Jan-14 11:29am    
If you can explain , the logic correctly , I ll try to modify the conditions accordingly.

Assuming the if conditional statements are correct.

Check the SKIP method used .

When page=0 and pagesize=10

Query becomes,

data = data.OrderByDescending(c => c.UploadDate).Skip(0).Take(10);//doesnt skip anything


When page=1 and pagesize=10
data = data.OrderByDescending(c => c.UploadDate).Skip(10).Take(10);//skips the everything which was intended to Take.


So the Count taken on the result may not fetch the desired results.

Modify the conditions accordingly.

Also, suggest to combine the multiple if conditions with if else if construct if possible with AND OR and combine the Linq query.
 
Share this answer
 
you write count after return this was not executed by the compiler first
C#
return Request.CreateResponse(HttpStatusCode.OK, data);
          int SelectedRows = data.Count();

but when you return how it will calculate count first and
Send it will along with data. other way to send it is:-
same opration as you to calculate data.
C#
data1 _data1  = new data1(); 
_data1._Data=data;
 _data1.SelectedRows = data.Count();

return Request.CreateResponse(HttpStatusCode.OK, _data1  );

you silple murged you data and count holder variable 
and used it any where you want.


C#
public class data1 
{
public List<typeofclassreturn> _Data{get;set;}
public  int SelectedRows{get;set;} 
}



i hope this helps you if not please comment on it.
 
Share this answer
 
v2
Comments
miss786 4-Feb-14 5:34am    
Thank you so much for your response. I little unclear of what you mean in your solution. I manage to update my code using your example but I am still unable to get the count to show up in the client-side by calling api/data?name=storm. Many thanks.
rizwan muhammed khan gouri 4-Feb-14 8:36am    
hi

i am just explain more. you can create a class public class data1
{
public List<typeofclassreturn> _Data{get;set;}
public int SelectedRows{get;set;}
}
any where in your project.which has one int property and second one list which is you return type class.
then in api method you can calculate your data and its count than assign it to the class object and return it.
for consumming whatever you using ajax or service refrence you get it as responce stream.


in ajax

onsuccess: function( response)
{
var count =respomse.SelectedRows;
var _obj=response._Data;
$.each()
{to itrate your _obj ....
}
}


you also check data in you browser

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900