在linq to sql中生成匿名类型时如何进行排序?
例如:
from e in linq0 order by User descending /* ??? */ select new { Id = e.Id, CommentText = e.CommentText, UserId = e.UserId, User = (e.User.FirstName + " " + e.User.LastName).Trim()), Date = string.Format("{0:d}", e.Date) }
Jon Skeet.. 20
如果你正在使用LINQ to Objects,我会这样做:
var query = from e in linq0 select new { Id = e.Id, CommentText = e.CommentText, UserId = e.UserId, User = (e.User.FirstName + " " + e.User.LastName).Trim()), Date = e.Date.ToString("d") } into anon orderby anon.User descending select anon;
这样,字符串连接只需要完成一次.
我不知道在LINQ to SQL中会做什么......
如果你正在使用LINQ to Objects,我会这样做:
var query = from e in linq0 select new { Id = e.Id, CommentText = e.CommentText, UserId = e.UserId, User = (e.User.FirstName + " " + e.User.LastName).Trim()), Date = e.Date.ToString("d") } into anon orderby anon.User descending select anon;
这样,字符串连接只需要完成一次.
我不知道在LINQ to SQL中会做什么......
如果我已正确理解您的问题,您希望这样做:
from e in linq0 order by (e.User.FirstName + " " + e.User.LastName).Trim()) descending select new { Id = e.Id, CommentText = e.CommentText, UserId = e.UserId, User = (e.User.FirstName + " " + e.User.LastName).Trim()), Date = string.Format("{0:d}", e.Date) }