Reputation: 815
I have a list of posts, and I have a list of opinions, 4 opinions users can vote for each post.
It's like:
POST1
Vote (opinion A | 10 votes), vote (opinion B | 2 votes), vote (opinion C | 1 votes), vote (opinion D | 0 votes)
POST2
Vote (opinion A | 3 votes), vote (opinion B | 4 votes), vote (opinion C | 5 votes), vote (opinion D | 7 votes)
For each post any user posts in his or her Diary, other users can vote 1 opinion.
In my controller I have this line to get the Diary Posts:
var diaryPosts = (from d in db.DiaryPosts
join e in db.EstadosDeAlma
on d.EstadosDeAlmaID equals e.ID
join u in db.User
on d.UserID equals u.ID
orderby d.ID descending
select new DiaryPostsSet
{
PostID = d.ID,
EstadoDeAlmaID = e.ID,
EstadoDeAlma = e.Title,
Author = u.Nickname,
Thumbnail = u.Thumbnail,
AuthorComment = d.Content,
Time = d.UpdateTime }).Take(6).ToList();
And I try to take for each DiaryPost, its opinions votes, and here is my problem. I have it like this:
List<ImpressionsSet> impressions = new List<ImpressionsSet>();
foreach (var item in diaryPosts)
{
impressions = (from i in db.Impressions
select new ImpressionsSet
{
ImpressionID = i.ID,
ImpressionTitle = i.Impression,
UrlSlug = i.UrlSlug,
DiaryPostID = item.PostID,
ImpressionNum = i.DiaryImpressions.Count(d => d.DiaryPostsID == item.PostID)
}).ToList();
}
But 'impressions' var gets only the last loop. I don't know how to solve that, cuz I had only a few experience with arrays and lists in C#4.0. I don't know if I could use that like impressions[n], I tried and it doesn't work.
I also need a sample code on how I would control the results of this loop in my Razor View. I have this now:
@foreach (var imp in ViewBag.ImpressionsList)
{
if (item.PostID == imp.DiaryPostID)
{
<td>
@{ string cbName = imp.UrlSlug; }
@{ string impression = imp.ImpressionTitle; }
@{ string value = imp.DiaryPostID + "|" + imp.ImpressionID + "|" + Session["id"].ToString(); }
<a class="voto" href="javascript:;" onclick="PostImpressions('@value')">@impression</a>
</td>
<td>
<a class="num" href="javascript:;" onclick="PostImpressions('@value')">@imp.ImpressionNum</a>
</td>
}
}
But I'm not sure if it would really work, cuz I dont have the results for the 2 posts in my impressions var, it just get the last result.
Could anyone help me?
Upvotes: 1
Views: 773
Reputation: 120927
instead of:
impressions = ...
Do
impressions.AddRange( (from i in db.Impressions
select new ImpressionsSet
{
ImpressionID = i.ID,
ImpressionTitle = i.Impression,
UrlSlug = i.UrlSlug,
DiaryPostID = item.PostID,
ImpressionNum = i.DiaryImpressions.Count(d => d.DiaryPostsID == item.PostID)
}).ToList() );
Upvotes: 1