Entity framework ASP NET MVC如何调用属性计数,然后打印出排序列表?

Entity framework ASP NET MVC如何调用属性计数,然后打印出排序列表?,entity-framework,asp.net-mvc-3,linq-to-entities,Entity Framework,Asp.net Mvc 3,Linq To Entities,假设我上过一节课: public class Post { public int PostId { get; set; } public string Topic { get; set; } public int UserId { get; set; } [StringLength(5000)] public string Body { get; set; } public DateTime DateCreated { get; set; }

假设我上过一节课:

public class Post
{
    public int PostId { get; set; }
    public string Topic { get; set; }
    public int UserId { get; set; }
    [StringLength(5000)]
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }
    public string Name { get; set; }
    public int Votes { get; set; }
} 
对于每个帖子,用户可以输入一个主题。例如,如果主题是“红色”、“绿色”、“蓝色”和“黄色”,我如何根据使用次数创建列表

输出示例:

Red   | 70
Blue  | 60
Green | 40
Yellow| 35
编辑:为什么这不起作用,并给我一个错误,我不能隐式转换类型

public List<string> GetPopularTopics(int count)
    {
        var posts = from p in db.Posts
                    group p by p.Topic into myGroup
                    select new
                    {
                        Topic = myGroup.Key,
                        Count = myGroup.Count()
                    };
        return posts.ToList();
    }
public List GetPopularTopics(整数计数)
{
var posts=从p到db.posts
按主题分组到myGroup中
选择新的
{
Topic=myGroup.Key,
Count=myGroup.Count()
};
返回柱。ToList();
}
编辑2:

所以我尝试了你的解决方案Dustin,我得到了一个错误。这就是我使用的:

public IEnumerable<IGrouping<string,int>> GetPosts()
    {
        var posts = from p in db.Posts
                    group p by p.Topic into topicCounts
                    select new
                    {
                        Topic = topicCounts.Key,
                        Count = topicCounts.Count()
                    };
        return posts.ToList();
    }
public IEnumerable GetPosts()
{
var posts=从p到db.posts
按主题将p分组为主题计数
选择新的
{
Topic=topicCounts.Key,
Count=topicCounts.Count()
};
返回柱。ToList();
}
这在posts.ToList()下给了我一个错误:
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable>”。存在显式转换(是否缺少强制转换?

以创建分组,然后创建匿名类型,例如:

var posts = from p in context.Posts
            group p by p.Topic into topicCounts
            select new
            {
                Topic = topicCounts.Key,
                Count = topicCounts.Count()
            };
然后,为了处理日期,让我们假设对其进行迭代:

foreach(var p in posts)
{
    Response.Write(String.Format("{0} - {1}", p.Topic, p.Count));
}

要创建分组,请创建匿名类型,例如:

var posts = from p in context.Posts
            group p by p.Topic into topicCounts
            select new
            {
                Topic = topicCounts.Key,
                Count = topicCounts.Count()
            };
然后,为了处理日期,让我们假设对其进行迭代:

foreach(var p in posts)
{
    Response.Write(String.Format("{0} - {1}", p.Topic, p.Count));
}

问题是您需要为返回值使用非匿名类型

此查询创建匿名类型的IEnumerable

var posts = from p in context.Posts
        group p by p.Topic into topicCounts
        select new
        {
            Topic = topicCounts.Key,
            Count = topicCounts.Count()
        };
创建匿名对象的是
selectnew
语句

你需要做的是创建一些非匿名的东西——一个可以在这个方法内外共享的对象

像这样:

public IEnumerable<TopicAndCount> GetPosts()
{
    var posts = from p in context.Posts
        group p by p.Topic into topicCounts
        select new TopicAndCount
        {
            Topic = topicCounts.Key,
            Count = topicCounts.Count()
        };
 }
public IEnumerable GetPosts()
{
var posts=来自context.posts中的p
按主题将p分组为主题计数
选择新TopicAndCount
{
Topic=topicCounts.Key,
Count=topicCounts.Count()
};
}
注意
selectnewtopicandcount
语句和封闭方法的返回值


这将解决您的问题。

问题是您需要使用非匿名类型作为返回值

此查询创建匿名类型的IEnumerable

var posts = from p in context.Posts
        group p by p.Topic into topicCounts
        select new
        {
            Topic = topicCounts.Key,
            Count = topicCounts.Count()
        };
创建匿名对象的是
selectnew
语句

你需要做的是创建一些非匿名的东西——一个可以在这个方法内外共享的对象

像这样:

public IEnumerable<TopicAndCount> GetPosts()
{
    var posts = from p in context.Posts
        group p by p.Topic into topicCounts
        select new TopicAndCount
        {
            Topic = topicCounts.Key,
            Count = topicCounts.Count()
        };
 }
public IEnumerable GetPosts()
{
var posts=来自context.posts中的p
按主题将p分组为主题计数
选择新TopicAndCount
{
Topic=topicCounts.Key,
Count=topicCounts.Count()
};
}
注意
selectnewtopicandcount
语句和封闭方法的返回值


这将解决您的问题。

如果执行投影并将其返回表单方法,则必须创建一个新类型

public class MyCounts
{
    public string Topic { get; set; }
    public int Count { get; set; }
}


public List<MyCounts> GetPopularTopics(int count)
{
    var posts = from p in db.Posts
                group p by p.Topic into myGroup
                select new MyCounts
                {
                    Topic = myGroup.Key,
                    Count = myGroup.Count()
                };
    return posts.ToList();
}
公共类MyCounts
{
公共字符串主题{get;set;}
公共整数计数{get;set;}
}
公共列表GetPopularTopics(整数计数)
{
var posts=从p到db.posts
按主题分组到myGroup中
选择新的MyCounts
{
Topic=myGroup.Key,
Count=myGroup.Count()
};
返回柱。ToList();
}

如果执行投影并将其返回表单方法,则必须创建一个新类型

public class MyCounts
{
    public string Topic { get; set; }
    public int Count { get; set; }
}


public List<MyCounts> GetPopularTopics(int count)
{
    var posts = from p in db.Posts
                group p by p.Topic into myGroup
                select new MyCounts
                {
                    Topic = myGroup.Key,
                    Count = myGroup.Count()
                };
    return posts.ToList();
}
公共类MyCounts
{
公共字符串主题{get;set;}
公共整数计数{get;set;}
}
公共列表GetPopularTopics(整数计数)
{
var posts=从p到db.posts
按主题分组到myGroup中
选择新的MyCounts
{
Topic=myGroup.Key,
Count=myGroup.Count()
};
返回柱。ToList();
}

每篇文章会有几个主题吗?就像这里的标签一样?不是每个帖子只有一个主题OK,Dustin Laine有下面的解决方案。每个帖子会有几个主题吗?就像这里的标签一样?不是每个帖子只有一个主题,Dustin Laine有下面的解决方案。那么,什么是查询示例?我遇到了无法隐式转换的错误问题,比如我会使用公共字符串吗?或者我将如何开始该查询方法?检查我的更新,不确定这是否是您所指的。我的意思是,如果我使用ASP.NET MVC3,我将如何从模型>控制器>视图?嗯,这没有帮助。EF查询将从数据库中获取数据,该代码应该在模型类中。控制器类应该将这个
posts
对象传递给您的视图,您的视图将在屏幕上进行写入。然而,有数百种方法可以实现这一点,这应该可以帮助您完成一个基本场景?我遇到了无法隐式转换的错误问题,比如我会使用公共字符串吗?或者我将如何开始该查询方法?检查我的更新,不确定这是否是您所指的。我的意思是,如果我使用ASP.NET MVC3,我将如何从模型>控制器>视图?嗯,这没有帮助。EF查询将从数据库中获取数据,该代码应该在模型类中。控制器类应该将这个
posts
对象传递给您的视图,您的视图将在屏幕上进行写入。然而,有数百种方法可以做到这一点,这应该