简体   繁体   English

具有多个from子句的linq查询的方法语法

[英]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. 我试图弄清楚如何将嵌套的from子句替换为方法语法。 I was trying with .Select or .SelectMany, but I didn't manage to get the same result. 我尝试使用.Select或.SelectMany,但没有获得相同的结果。

  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. 删除它们可能是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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