mvc - Cómo pedir el artículo virtual en el marco de la entidad

For example I have 2 models that Tema y Comentario.
1 Topic can have some Comment, so in my Topics class I have an ICollection feid:

public class Topic
    public virtual ICollection<Comment> Comments { get; set; }

And I call it by this stament:

var topic = db.Topics.Include("Comments").Single(d => d.TopicId == someid)

The code will load for me all the Comentario thats belong to my current Tema, so I can print all my comment out by this code:

@foreach (var comment in topic.Comments)
    <div>@comment.CommentContent</div><hr />

The issue is I want to re-oder my Comments list, but I donot know HOW! Please some one show me the right way!
Thanks all of you so much!

preguntado el 08 de noviembre de 11 a las 13:11

1 Respuestas

@foreach (var comment in topic.Comments.OrderBy(c=>c.PropertyYouWantToOrderBy))
<div>@comment.CommentContent</div><hr />

respondido 08 nov., 11:18

When I do as you say, I got a Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type - nvcnvn

are your view strongly typed? is "topic" dynamic ? - Rouen

No! In fact I too lazy so I have set ViewBag.topic = db.Topics.Include("Comments").Single(d => d.TopicId == someid); so I guest now I should do it as the traditional way?! - nvcnvn

dont use ViewBag for domain model objects... bad bad nvcnvn ! :) create a ViewModel - Rouen

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.