AlexCode
AlexCode

Reputation: 4123

Can I use OrderBy<T> to order by a Join<T> entity?

I need to translate a Linq query into Dynamic Linq but I'm having a problem with the OrderBy part.

This is the Linq query:

var q = from pp in ctx.MyEntity
        join c in sortedListString on pp.CountryId.ToString() equals c.Substring(9)
        orderby c ascending
        select pp;

As you can see, I'm sorting by the entinty in the join.
Now I need to write the same query but with dynamic linq and I have:

var q = from pp in ctx.MyEntity
        select pp;

q = q.Join(
    sortedListString, 
    o => o.CountryId.ToString(), 
    i => i.Substring(9), (o, i) => o
).OrderBy(???);

What should I put in the OrderBy, knowing that I want to order by sortedListString?

Upvotes: 1

Views: 66

Answers (1)

Evk
Evk

Reputation: 101453

Not sure what you mean by "dynamic linq" here, but equivalent of your query using "full" LINQ syntax is:

var q = ctx.MyEntity
    .Join(sortedListString, 
        pp => pp.CountryId.ToString(), 
        c => c.Substring(9), 
        (pp, c) => new { pp, c })
    .OrderBy(t => t.c)
    .Select(t => t.pp);

Upvotes: 1

Related Questions