开发者

The best way to get read only data using EF and support sporting/searching/filtering

开发者 https://www.devze.com 2023-02-14 20:47 出处:网络
Lets say that I need to execute this query with EF in business layer var list = context.Invoices.Select(x => new

Lets say that I need to execute this query with EF in business layer

var list = context.Invoices.Select(x => new
{
    InvoiceNumber = x.InvoiceNUmber,
    InvoiceDate = x.InvoiceDate,
    CustomerName = x.Customer.Custom开发者_如何学PythonerName,
    TotalValue = x.InvoiceData.Sum(y => y.Quantity * y.Price),
    Id = x.Id
}).ToList();

What can I do for this list to be easily sortable, searchable or filterable in UI layer?

Thanks, Goran


The way to do it is to return a object that is not anonymous. That is, create a class to hold this data. It should either have an implicit zero-argument constructor (because you have no constructors), or an explicit zero-argument one (because you have defined other constructors). Then you can say:

List<MyObject> list = context.Invoices.Select(x => new MyObject()
{
    InvoiceNumber = x.InvoiceNUmber,
    InvoiceDate = x.InvoiceDate,
    CustomerName = x.Customer.CustomerName,
    TotalValue = x.InvoiceData.Sum(y => y.Quantity * y.Price),
    Id = x.Id
}).ToList();

Now you can return the strongly-typed list of objects out of your business layer, and your UI layer can use LINQ (or whatever) to do sorting/filtering/paging.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号