dudeNumber4
dudeNumber4

Reputation: 4737

EF OrderBy method doesn't work with joins

Following works (ordered by name):
from t in context.Table1.OrderBy( "it.Name" ) select t

This doesn't work (no ordering):
from t in context.Table1.OrderBy( "it.Name" )
join t2 in context.Table2 on t.SomeId equals t2.SomeId select t

Nor does this (trying to reference the parent table to order):
from t in context.Table1
join t2 in context.Table2.OrderBy( "it.Table1.Name" ) on t.SomeId equals t2.SomeId select t

Nor does this (trying to order on the child table):
from t in context.Table1
join t2 in context.Table2.OrderBy( "it.ChildName" ) on t.SomeId equals t2.SomeId select t

How do I cause OrderBy not to be ignored while joining?

Upvotes: 0

Views: 1123

Answers (3)

Craig Stuntz
Craig Stuntz

Reputation: 126587

This will work:

(from t in context.Table1
 join t2 in context.Table2 on t.SomeId equals t2.SomeId 
 select t).OrderBy( "it.Name" );

However, you should not be using join at all, as @moi_meme comments.

Upvotes: 1

Andy_Vulhop
Andy_Vulhop

Reputation: 4779

You might try ordering after the join, instead of before/during.

from t in context.Table1
join t2 in context.Table2 
on t.SomeId equals t2.SomeId 
orderby t.Name
select t

Upvotes: 0

mt0321
mt0321

Reputation: 105

For your second example, try:

var res = from t in context.Table1
          join t2 in context.Table2 on t.SomeId equals t2.SomeId 
          orderby t.Name
          select t;

This should order the joined results.

Upvotes: 0

Related Questions