linq to sql order by
How to order result by nested table column like
FROM a IN Repository
FROM b开发者_StackOverflow中文版 IN a.Users
ORDERBY b.Name
SELECT a
But it does not work, i want to select all in 'a' table whitch is order by Name column in relation table 'b', how can I? Thanks.
UPD: I'm very sorry. My fault is my thoughtlessness. Probably, i didn't notice the post processing the list of elements from result where it ordered again. Thanks for all guys!
Not sure if this is the best way, but I would do this
var query = from ab in
(from a in Repository
select new
{
A = a,
B = a.Users.OrderBy(u => u.Name).FirstOrDefault()
})
orderby ab.B.Name
select ab.A;
Is there a relation between the tables? Do you have navigation properties? In that case, use:
from a in repository.a
orderby a.Users.Name
select a;
You can also add a property to an partial class, and then use the other property instead.
public partial class Repository
{
public IOrderedEnumerable<User> OrderedUser
{
get
{
return this._User.OrderByDescending(u => u.Name);
}
}
}
精彩评论