c# - MVC Foreach distinct value from database -
c# - MVC Foreach distinct value from database -
i have table in database looks bit this:
links
linksid
tvid(foreign key)
season
episode
link
now i'm trying have foreach statement in view on page.
season 1
episode 1
episode 2
episode 3
season 2
episode 1
episode 2
episode 3
however can
season 1 episode 1
season 1 episode 2
season 1 episode 3
season 2 episode 1
season 2 episode 2
season 2 episode 3
so after googling have got foreach display first episode not i'm after.
@foreach (var item in model.links.groupby(x => x.season).select(s => s.first())) { <p>season @html.displayfor(modelitem => item.season) @html.displayfor(modelitem => item.episode)</p> }
what doing wrong?
you want this:
@{ var mylist = model.links .groupby(x => x.season) .select(x => new { season = x.key, episodes = x }); } @foreach (var season in mylist) { <strong>season @html.displayfor(modelitem => season.season)</strong> foreach(var episode in season.episodes) { @html.displayfor(modelitem => item.episode) } }
c# asp.net-mvc linq
Comments
Post a Comment