thalm
thalm

Reputation: 2920

How to join two arrays in a LINQ query and use the result further in the query?

Sorry for the noobish question, i have no idea about linq and have to extend a query in a code which is not from me.

Is it possible to join two enumerables which are results of two different method calls into one enumerable?

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 };

Thanks a lot!

Upvotes: 1

Views: 1815

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564323

You can use Enumerable.Concat to concatenate two IEnumerable<T> sequences, however, in your case, the two objects are of different types, so this will not work properly without also using Enumerable.Cast<T>.

You could potentially treat both as MemberDescriptor via:

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 };

Upvotes: 3

Related Questions