Count属性根据其他总计进行初始化

时间:2012-12-30 23:43:37

标签: 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 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; }
}

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

1 个答案:

答案 0 :(得分:1)

你应该能够做到这一点。您只需要为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.
    }
}

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