Thiago Custodio Thiago Custodio - 7 months ago 50
C# Question

method syntax for linq query with multiple from clauses

I was trying to figure out how to replace the nested from clause to a method syntax. I was trying with .Select or .SelectMany, but I didn't manage to get the same result.

var query = (from DirectToStoreStore s in dtsOrder.Stores
from DirectToStoreProduct p in s.Products
where p.DirectToStoreOrderLineID == directToOrderLineID
select p);


There's plenty of ways you could write it.

var query = dtsOrder.Stores.Cast<DirectToStoreStore>()
    .SelectMany(s => s.Products.Cast<DirectToStoreProduct>()
        .Where(p => p.DirectToStoreOrderLineID == directToOrderLineID)

Though the casts may not be necessary, but they're only there since you explicitly declared them in your query. It'll probably be safe to remove them.