I'm building a music voting application, similar to stack overflow almost.
I have 3 tables, Charts, ChartItems, Votes.
I'm trying to bring back a list of chartitems, which link to a single chart, with the number of votes for each chart item counted.
This is what I am trying currently
var firstList = from chartItem in db.ChartItems
join vote in db.Votes on chartItem.ixChartId equals vote.ixChartId into j1
where chartItem.ixChartId == id
from j2 in j1.DefaultIfEmpty()
group j2 by chartItem.ixChartItemId into grouped
select new ChartItemWithVotes
{
totalVotes = grouped.Count(t => t.ixVoteId != null),
//CANT GET OTHER PROPS HERE
};
The problem is once I have performed the grouping, I can't get any of the other properties I need from the join to populate the model. For example each ChartItem should have a title, id etc...
I have created a ViewModel to hold all the properties that I need, called ChartItemWithVotes(includes all entity values + int totalVotes)
Can anyone help me with where I am going wrong.
In the end I am looking for this
Chart Name
Votes Name
20 - ChartItemname
15 - ChartItemname
12 - ChartITemName
This may not be the answer you're looking for, but if the appropriate relationships are set up in SQL server, and therefore have been imported into the DBML, you should be able to do the following:
var chartvotes = from chartItem in db.ChartItems
select new {
ChartItem = chartItem,
TotalVotes = chartItem.Votes.Count()
};