【问题标题】:How to join two arrays in a LINQ query and use the result further in the query?如何在 LINQ 查询中连接两个数组并在查询中进一步使用结果?
【发布时间】:2013-06-27 22:33:23
【问题描述】:

对不起,这个菜鸟问题,我不知道 linq,必须在不是我的代码中扩展查询。

是否可以将两个不同方法调用的结果的两个可枚举连接到一个可枚举中?

var attibutes = from MemberDescriptor a in
     (TypeDescriptor.GetProperties(this) **and** TypeDescriptor.GetEvents(this))
      let attribute = a.Attributes[typeof(MyAttributeAttribute)] 
                                as MyAttributeAttribute
      where attribute != null
       select new AttributeTuple { Property = a, Attribute = attribute };

非常感谢!

【问题讨论】:

    标签: c# .net linq join


    【解决方案1】:

    您可以使用Enumerable.Concat 连接两个IEnumerable<T> 序列,但是,在您的情况下,这两个对象属于不同类型,因此如果不同时使用Enumerable.Cast<T>,这将无法正常工作。

    您可以通过以下方式将两者都视为MemberDescriptor:

    var search = TypeDescriptor.GetProperties(this).Cast<MemberDescriptor>()
                   .Concat(TypeDescriptor.GetEvents(this).Cast<MemberDescriptor>());
    
    var attibutes = from MemberDescriptor a in search
                let attribute = a.Attributes[typeof(MyAttributeAttribute)] as MyAttributeAttribute
                where attribute != null
                select new AttributeTuple { Property = a, Attribute = attribute };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2013-03-26
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多