开发者

LINQ to Entities - multiple OrderBy methods do not work

If I apply two OrderBy methods to my query, like that

query.OrderBy(rec => rec.Name).OrderByDescending(rec => rec.Title);

Then only second method is applied, the first one is ignored. Is it a bug? What if I need to have ascending ordering for one column and descending ordering for another? Is it开发者_运维百科 not achievable at all by method syntax?


Try this:

query.OrderBy(rec => rec.Name).ThenByDescending(rec => rec.Title);

Your second OrderBy is reseting your first result set. That's why the ThenBy extension exists. It will preserve your first result set, while applying additional sorting to it.

Essentially, your existing solution as psuedo SQL would look something like this:

results = SELECT * FROM Obj ORDER BY Name;
results = SELECT * FROM results ORDER BY Title DESC;

...which isn't what you want. The ThenBy extension would look something like this:

results = SELECT * FROM Obj ORDER BY Name ASC, Title DESC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜