Support forum for ASP.NET Zero (https://aspnetzero.com/).
By smartlayer
#32007 Hi,

We have a jtable which have some columns whose value is calculated based on some conditions.

For example-
Code: Select allvar documents = await documentsQuery
    .OrderBy(input.Sorting)
    .PageBy(input)
    .Select(x => new DocumentListDto
    {
        Id = x.Id,
        Title = x.Title,
        File_Size_Presenter = x.File_Size + " KB",
        Document_Approval_Type = x.Entity_Document_Approval_Types.Approval_Type,
        Document_Approval_Type_ID_FK = x.Document_Approval_Type_ID_FK,
        CreationTime = x.CreationTime,
        Approvers = x.Entity_Approvers.Count(),
        Status = x.Is_Publish ? "Published" : "Pending",
        Status2 = x.Is_Publish ? "Document has been published" : "Pending Approval"
        Author = x.AbpUser_OriginalWriterUser.Name + " " + x.AbpUser_OriginalWriterUser.Surname,
        DocumentAdmin = x.AbpUser_DocumentAdminUser.Name + " " + x.AbpUser_DocumentAdminUser.Surname
    })
    .ToListAsync();


Here, columns like Status, Status2, Author, DocumentAdmin does not exists in database.
Applying filter on them would mean to first fetch all data and then filter it, which does not seem like any efficient way.
We are also using paging and sorting and we cannot lose this functionality. (Sorting is not working on calculated columns. So, as a bonus, if you have a solution to that, it'll be much appretiated :) )


I believe many of you, like me, would have banged your head on wall to do this correctly.
So, how could this be handled in an elegant way?

Thanks!
User avatar
By alper
#32013 Hi,

Why don't you try to add whereIf in your query.

Code: Select allprivate void GetMyList(bool? isPublished)
{
     var documents = await documentsQuery
.WhereIf(isPublished.HasValue, x=> x.Is_Publish = isPublished.Value)
    .OrderBy(input.Sorting)
    .PageBy(input)
    .Select(x => new DocumentListDto
    {
        Id = x.Id,
        Title = x.Title,
        File_Size_Presenter = x.File_Size + " KB",
        Document_Approval_Type = x.Entity_Document_Approval_Types.Approval_Type,
        Document_Approval_Type_ID_FK = x.Document_Approval_Type_ID_FK,
        CreationTime = x.CreationTime,
        Approvers = x.Entity_Approvers.Count(),
        Status = x.Is_Publish ? "Published" : "Pending",
        Status2 = x.Is_Publish ? "Document has been published" : "Pending Approval"
        Author = x.AbpUser_OriginalWriterUser.Name + " " + x.AbpUser_OriginalWriterUser.Surname,
        DocumentAdmin = x.AbpUser_DocumentAdminUser.Name + " " + x.AbpUser_DocumentAdminUser.Surname
    })
    .ToListAsync();

}