C#MVC4变量,用于保存两个可更新的列

C#MVC4变量,用于保存两个可更新的列,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在为我的MVC4 webapp(C#)编写一个方法,该方法将获得一个博客对象列表,并给出一个两列的类别列表及其点击率。我提供的代码存在的问题是KeyValuePairs是不可变的。最终目标是使用此变量(修剪到前6行/对)显示顶级类别(根据该类别中的任何文章的浏览次数)。 我应该使用不同的变量类型还是以不同的方式进行更新 public List<KeyValuePair<string, int>> Top6BlogCategories() { /

我正在为我的MVC4 webapp(C#)编写一个方法,该方法将获得一个博客对象列表,并给出一个两列的类别列表及其点击率。我提供的代码存在的问题是KeyValuePairs是不可变的。最终目标是使用此变量(修剪到前6行/对)显示顶级类别(根据该类别中的任何文章的浏览次数)。 我应该使用不同的变量类型还是以不同的方式进行更新

public List<KeyValuePair<string, int>> Top6BlogCategories()
    {
        // Get Blog posts
        IEnumerable<Blog> posts = db.Blogs.ToList();

        // Get Blog Categories
        IEnumerable<string> categories = Categories();

        // Create a variable to hold the Category and its Hits
        List<KeyValuePair<string, int>> categoryHits = new List<KeyValuePair<string,int>>();

        // Populate the List's first column (Category)
        foreach(var category in categories)
        {
            categoryHits.Add(new KeyValuePair<string, int>(category, 0));
        }

        // Populate the List's second column (Hits)
        foreach(var row in categoryHits)
        {
            foreach(var post in posts)
            {
                if(post.Category == row.Key)
                {
                    row.Value++;
                }
            }
        }

        return categoryHits;
    }
公共列表Top6BlogCategories()
{
//获取博客帖子
IEnumerable posts=db.Blogs.ToList();
//获取博客类别
IEnumerable categories=categories();
//创建一个变量来保存类别及其点击
List categoryHits=新列表();
//填充列表的第一列(类别)
foreach(类别中的var类别)
{
添加(新的KeyValuePair(类别,0));
}
//填充列表的第二列(点击数)
foreach(类别中的变量行)
{
foreach(var post in post)
{
if(post.Category==row.Key)
{
row.Value++;
}
}
}
返回类别;
}

您应该使用ViewModel来描述您期望的响应

例如,如果您希望向您的视图发送前6个类别及其全部帖子的列表,我将使用以下内容:

视图模型:

public class TopCategoryModel
{
    public string Category {get; set;}
    public int Count {get; set;}
}
控制员的行动:

public IEnumerable<TopCategoryModel> Top6BlogCategories()
{
    return db.Blogs
        .Select(b => b.Category)
        .Distinct()
        .Select(c => new TopCategoryModel
            {
                Category = c,
                Count = db.Blogs.Count(b => b.Category == c)
            })
        .OrderByDescending(a => a.Count)
        .Take(6);
}
public IEnumerable Top6BlogCategories()
{
返回db.Blogs
.选择(b=>b.类别)
.Distinct()
.选择(c=>new TopCategoryModel
{
类别=c,
Count=db.Blogs.Count(b=>b.Category==c)
})
.OrderByDescending(a=>a.Count)
.采取(6);
}

您可以使用LINQ和Count吗

public IEnumerable<Blog> Top6BlogCategories()
{
    // Get Blog posts
    IEnumerable<Blog> posts = db.Blogs.ToList();

    // Get Blog Categories
    IEnumerable<string> categories = Categories();

    // Create a variable to hold the Category and its Hits
    List<KeyValuePair<string, int>> categoryHits = new List<KeyValuePair<string,int>>();

    // Populate the List's first column (Category)
    foreach(var category in categories)
    {
        categoryHits.Add(new KeyValuePair<string, int>(category, posts.Count(post => post.Category == category)));
    }

    return posts;
}
public IEnumerable Top6BlogCategories()
{
//获取博客帖子
IEnumerable posts=db.Blogs.ToList();
//获取博客类别
IEnumerable categories=categories();
//创建一个变量来保存类别及其点击
List categoryHits=新列表();
//填充列表的第一列(类别)
foreach(类别中的var类别)
{
添加(新的KeyValuePair(category,posts.Count(post=>post.category==category));
}
返回岗位;
}

您可以使用
字典
,但您需要在不使用
foreach
的情况下对其进行迭代,因为这会导致它是只读的。@Eval这与我使用KeyValuePair时遇到的问题相同。不一样,只是相似,您接受的答案是一种更好的方法。但是
KeyValuePair
是不可变的,您实际上无法更改其中的数据
Dictionary
不是不可变的,但是一个
foreach
循环使它成为
只读的
,这是不同的。您可以循环使用
字典
,而无需使用
foreach
,这样您就可以更改字典中的值。