[something]Set class - why LINQ extension methods are not available

[something]Set class - why LINQ extension methods are not available

adam.krug
Advocate Advocate
600 Views
2 Replies
Message 1 of 3

[something]Set class - why LINQ extension methods are not available

adam.krug
Advocate
Advocate

Hi all,

 

Last time I needed to extract the first element of ConnectorSet class, which implements IEnumerable interface. Thus I expected I could use LINQ extension method like:

 

myConnectorSet.FirstOrDefault()

 

But it's not available, why is that?

 

PS1: I used "using System.Linq"

PS2: I managed to get the first element with foreach loop, I'm only asking because I want to understand why LINQ methods are not available for *Set classes in Revit API

0 Likes
Accepted solutions (1)
601 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

The generic .NET LINQ methods require specific interfaces, e.g., IEnumerable.

 

The old-fashioned Revit API *Set implementation probably do not implement the required interfaces.

 

They are gradually being phased out and replaced by generic .NET collections anyway (and for that reason, among others).

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 3

BobbyC.Jones
Advocate
Advocate
Accepted solution

Because .FirstOrDefault() is an extension of IEnumerable<T>, not IEnumerable.  There are a number of options to get the first element out of the ConnectorSet, the easiest on my eyes is something like this:

 

var myFirstConnector = myConnectorSet.Cast<Connector>().FirstOrDefault();
--
Bobby C. Jones