none
Forum - ado.net query RRS feed

  • Question

  • I have two tables:

    TOPICS
    TopicId
    Subject

    POSTS
    PostId
    TopicId
    AuthorId
    Content
    SendDate

    One topic can have many posts. I need to get topics order by last reply in topic (SendDate in table Posts).

    My query:

     

    from x1 in Db.DataContext.Topics
    //orderby ??
    select x1


     


    Saturday, July 2, 2011 2:42 PM

Answers

  • Hi green_green;

    You can not reorder the collection that is attached to the topics table so what you can do is to create a result set that has the topic and also contains a list of post ordered in the way you need them.

    var query = from t int DataContext.Topics
          where t.CategoryId == CategoryId
          select new
          {
          	topic = t,
          	post = t.Posts.OrderByDescending( p => p.SendDate )
          }
    

     

     

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 7:19 PM

All replies

  • I can't make in that way:

     

    from x1 in Db.DataContext.Topics
    where x1.CategoryId == CategoryId
    orderby x1.Posts.SendDate descending //here error
    select x1


     

    because then I have an error: Models.Posts does not contain a definition for SendDate - so what can I do ?


    Saturday, July 2, 2011 6:34 PM
  • Hi green_green;

    You can not reorder the collection that is attached to the topics table so what you can do is to create a result set that has the topic and also contains a list of post ordered in the way you need them.

    var query = from t int DataContext.Topics
          where t.CategoryId == CategoryId
          select new
          {
          	topic = t,
          	post = t.Posts.OrderByDescending( p => p.SendDate )
          }
    

     

     

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 7:19 PM