Asp.net mvc 基于其他总计初始化计数属性

Asp.net mvc 基于其他总计初始化计数属性,asp.net-mvc,Asp.net Mvc,我想知道这是否有意义 我有一个DTO模型: public class CategoryListByKeywordsDetailDto : ILocalizable { public int Id { get; set; } public string Name { get; set; } public string NameEN { get; set; } public string NameES { get; set; } public string N

我想知道这是否有意义

我有一个DTO模型:

public class CategoryListByKeywordsDetailDto : ILocalizable
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string NameEN { get; set; }
    public string NameES { get; set; }
    public string NameFR { get; set; }
    public string NamePT { get; set; }
    public int SearchCount { get; set; }
    public string ListController { get; set; }
    public string ListAction { get; set; }
}
名单是什么

public class CategoryListByBeywordsListDto
{
    public CategoryListByBeywordsListDto()
    {
        CategoryListByKeywordsDetails = new List<CategoryListByKeywordsDetailDto>();
    }

    public IList<CategoryListByKeywordsDetailDto> CategoryListByKeywordsDetails { get; set; }

    public int TotalSearchCount { get; set; }
}
公共类CategoryListByBeyWordsListTo
{
公共类别列表ByBeyWordsListTo()
{
CategoryListByKeywordsDetails=新列表();
}
公共IList CategoryListByKeywordsDetails{get;set;}
公共整数TotalSearchCount{get;set;}
}

我是否可以根据
CategoryListByKeywordsDetails
的列表
SearchCount
实时计算
TotalSearchCount
属性?

您应该能够做到这一点。您只需要实现TotalSearchCount的get方法。试着这样做:

public class CategoryListByBeywordsListDto
{
    public CategoryListByBeywordsListDto()
    {
        CategoryListByKeywordsDetails = new List<CategoryListByKeywordsDetailDto>();
    }

    public IList<CategoryListByKeywordsDetailDto> CategoryListByKeywordsDetails { get; set; }

    public int TotalSearchCount 
    { 
        get
        {
            if(CategoryListByKeywordsDetails != null)
            {
                return CategoryListByKeywordsDetails.Sum(x => x.SearchCount);
            }

            return 0;
        }

        //leave out set.
    }
}
公共类CategoryListByBeyWordsListTo
{
公共类别列表ByBeyWordsListTo()
{
CategoryListByKeywordsDetails=新列表();
}
公共IList CategoryListByKeywordsDetails{get;set;}
公共整数总搜索计数
{ 
得到
{
if(CategoryListByKeywordsDetails!=null)
{
返回CategoryListByKeywordsDetails.Sum(x=>x.SearchCount);
}
返回0;
}
//留下一套。
}
}

如果您的Dto上有多个客户端进程更新SearchCount,那么您将遇到更多的问题。如果您需要在没有用户交互的情况下(即刷新按钮)在客户端上进行更新,那么您需要实现定期轮询数据库并将值推送到客户端。更复杂的是,并不是真正的堆栈溢出问题可以回答的问题。

谢谢,我会尝试一下。