【发布时间】:2010-09-14 14:10:41
【问题描述】:
是否可以在 LINQ 查询中进行强制转换(为了编译器)?
下面的代码并不糟糕,但如果能把它变成一个查询就好了:
Content content = dataStore.RootControl as Controls.Content;
List<TabSection> tabList = (from t in content.ChildControls
select t).OfType<TabSection>().ToList();
List<Paragraph> paragraphList = (from t in tabList
from p in t.ChildControls
select p).OfType<Paragraph>().ToList();
List<Line> parentLineList = (from p in paragraphList
from pl in p.ChildControls
select pl).OfType<Line>().ToList();
代码继续执行更多查询,但要点是我必须从每个查询中创建一个列表,以便编译器知道content.ChildControls 中的所有对象都是TabSection 类型和t.ChildControls 中的所有对象的类型都是 Paragraph...等等。
在 LINQ 查询中是否有办法告诉编译器 from t in content.ChildControls 中的 t 是 TabSection?
【问题讨论】: