Linq groupby and count operations in mvc
So here is my table
MyCourse (MyCourseID, CourseNumber, CourseName, SemesterName, Year)
Now if the table consists of the following data
ENG1200 'Basic English' Spring 2011
ENG1200 'Basic English' Spring 2011
I am simply trying to extract the count along with the CourseNumber as follows
ENG1200 'Basic English' Spring 2011 2
and then insert it into a new strongly typed class so I can use it in my View. But the linq statement prints every class with a count of 1 on each.
ENG1200 'Basic English' Spring 2011 1
ENG1200 'Basic English' Spring 2011 1
Any help will be greatly appreciated.
Here is my linq code that I am using:
public IQueryable<VotedCourses> GetVoted_Courses()
{
var votedCourses = courseDB.MyCourses.GroupBy
(x => new
{
x.MyCourseID,
x.CourseNumber,
x.CourseName,
x.SemesterName,
x.Year
})
.Select(x =>
new VotedCourses
{
MyCourseID = x.Key.MyCourseID,
CourseNumber = x.Key.CourseNumber,
CourseName = x.Key.CourseName,
SemesterName = x.Key.SemesterName,
Year = x.Key.Year,
VoteCount = x.Count()
});
retu开发者_开发问答rn votedCourses;
}
I notice you're including x.MyCourseID
in your anonymous type. This column isn't present in your samples, and it looks like an identity. My guess is that this column is different between the two records. I'd remove it from the type and try again.
精彩评论