简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM