Moji
Moji

Reputation: 1830

How can I order an IQueryable based on a field that is in another table?

I am a newbie to LINQ and c# and I have two tables :

Products

ProductStoreds

ProductStore has a foreignKey to Products with a field named ProductId(same for both tables). I have an IQueryable named result from Products table. how can I sort it based on a field named status in ProductStore Table. this is what I have tried and I have not been successful:

result = result.Where(p =>
                p.ProductId == DbContext.ProductStores.Select(m => m.Product)
                .OrderByDescending(m => m.Status).ToInt());

Upvotes: 0

Views: 86

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can join and order by Product like this

var stores = DbContext.ProductStores;
result = from store in stores
        join product in result on product.ProductId equals store.ProductId
        orderby store.Status
        select product;

Upvotes: 1

Related Questions