【问题标题】:Doing a Cast Within a LINQ Query在 LINQ 查询中进行强制转换
【发布时间】: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 中的 tTabSection

【问题讨论】:

    标签: c# .net linq .net-3.5


    【解决方案1】:

    试试这个:

    from TabSection t in content.ChildControls
    

    此外,即使这不可用(或者对于您可能遇到的不同的未来场景),您也不会被限制将所有内容转换为列表。转换为列表会导致当场查询评估。但是,如果您删除 ToList 调用,则可以使用 IEnumerable 类型,它会继续推迟查询的执行,直到您实际迭代或存储在真实容器中。

    【讨论】:

      【解决方案2】:

      是的,您可以执行以下操作:

      List<TabSection> tabList = (from t in content.ChildControls
                                  where t as TabSection != null
                                  select t as TabSection).ToList();
      

      【讨论】:

      • 这就是 OfType() 的用途。
      【解决方案3】:

      根据您要执行的操作,其中一种可能会奏效:

      List<Line> parentLineList1 =
        (from t in content.ChildControls.OfType<TabSection>()
         from p in t.ChildControls.OfType<Paragraph>()
         from pl in p.ChildControls.OfType<Line>()
         select pl).ToList();
      
      List<Line> parentLineList2 =
        (from TabSection t in content.ChildControls
         from Paragraph p in t.ChildControls
         from Line pl in p.ChildControls
         select pl).ToList();
      

      请注意,您使用的是 OfType()。这将过滤结果并仅返回指定类型的项目。第二个查询隐式使用 Cast(),它将结果转换为指定的类型。如果任何项目无法转换,则会引发异常。正如 Turbulent Intellect 所提到的,您应该尽可能避免调用 ToList(),或者尽量避免它。

      【讨论】:

        【解决方案4】:

        这是查询方法表单。

        List<Line> parentLineList =
          content.ChildControls.OfType<TabSections>()
            .SelectMany(t => t.ChildControls.OfType<Paragraph>())
            .SelectMany(p => p.ChildControls.OfType<Line>())
            .ToList();
        

        【讨论】:

          【解决方案5】:
          List<TabSection> tabList = (from t in content.ChildControls
                                      let ts = t as TabSection
                                      where ts != null
                                      select ts).ToList();
          

          【讨论】:

          • 这就是 OfType() 的用途。
          • 对 OfType() 的反思让我得出结论,它的效率低于我的解决方案。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多