返回记录总数 [英] returning total count of records
本文介绍了返回记录总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WCF中的方法是
[WebGet(UriTemplate =GiveMeNamesOfStudents,ResponseFormat = WebMessageFormat.Json)]
public List< MyDataClass> GiveMeNamesOfStudentsList()
{
var returnData =(from myentity in myEntityRepository.AsQueryable()
选择新的MyDataClass
{
Id = myentity.Id,
Name = myentity.Name
})。Skip(PageSize * PageNo).Take(PageSize).ToList();
return returnData;
}
如何将实际数据的总记录数与数据一起返回? / p>
解决方案
我建议返回一个 DTO 包含您的列表和总计数。
您可以添加其他信息,例如页数,但我会保持简单。
首先创建您的通用 DTO ,所以你可以用其他方法。
public class ListResult< T>
{
public List< T>数据{get;组; }
public int TotalCount {get;组; }
public int Page {get;组; }
}
现在返回对象
[WebGet(UriTemplate =GiveMeNamesOfStudents,ResponseFormat = WebMessageFormat.Json)]
public ListResult< MyDataClass> GiveMeNamesOfStudentsList()
{
var returnData =(from myentity in myEntityRepository.AsQueryable()
选择新的MyDataClass
{
Id = myentity.Id,
Name = myentity.Name
})。Skip(PageSize * PageNo).Take(PageSize).ToList();
返回新的ListResult< MyDataClass>
{
Data = returnData,
TotalCount = myEntityRepository.Count(),
Page = PageNo
};
}
I am using WCF rest service and i want to return total record count just as we have inlinecount in webapi or breeze.
My method in WCF is
[WebGet(UriTemplate = "GiveMeNamesOfStudents", ResponseFormat = WebMessageFormat.Json)]
public List<MyDataClass> GiveMeNamesOfStudentsList()
{
var returnData = (from myentity in myEntityRepository.AsQueryable()
select new MyDataClass
{
Id = myentity.Id,
Name = myentity.Name
}).Skip(PageSize * PageNo).Take(PageSize).ToList();
return returnData;
}
How can i return total record count of my actual data together with data?
解决方案
I suggest to return a DTO containing your list and the total count.
You can add other informations like the page count for example but i will keep it simple here.
First create your generic DTO so you can use it in other methods too.
public class ListResult<T>
{
public List<T> Data { get; set; }
public int TotalCount { get; set; }
public int Page{ get; set; }
}
Now return the object
[WebGet(UriTemplate = "GiveMeNamesOfStudents", ResponseFormat = WebMessageFormat.Json)]
public ListResult<MyDataClass> GiveMeNamesOfStudentsList()
{
var returnData = (from myentity in myEntityRepository.AsQueryable()
select new MyDataClass
{
Id = myentity.Id,
Name = myentity.Name
}).Skip(PageSize * PageNo).Take(PageSize).ToList();
return new ListResult<MyDataClass>
{
Data = returnData,
TotalCount = myEntityRepository.Count(),
Page = PageNo
};
}
这篇关于返回记录总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文