I am wondering which one of these two is better to use:
1: context.Threads.Where(thread => thread.Id == threadId).Select(thread => thread.Posts).(...)
or
2: context.Posts.Where(post=> post.thread.Id == threadId).Select().(...)
Is there any difference between two?
You would need to check also, in addition to how many threads and posts there are, what SQL is being generated by both queries, and how long they each take in SQL Profiler.
On a side note, I question both of your queries. I’m assuming that you’re using Entity Framework (based on the other questions I’ve seen you ask), and so your Post class should have a ThreadID already on that table (based on the navigational property that I see here, you should have a ThreadID field on your Post table in your database). In which case the following query might be better suited for your needs:
By doing this you will remove any mention of the Threads table, which will mean that EF will not have to use any join statements to get the information that you’re requesting. Since it’s not going to include that Threads table, this should be the fastest way to go to get all posts from a single thread.