In fact some code about this technique was already shown in an article, Parameter of Revit API – 15: Set FamilyParameter, which was posted a bit earlier. Some readers may have noticed it.
And this can be an addition to the three approaches of converting Set to List as introduced in another article, Parameter of Revit API – 3: Convert ParameterSet to List of Parameter, and I would vote this approach as the best so far.
Here is the generic way to do the conversion:
public static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}
The method works with any enumerable Set or Collection instances regardless they are from Revit API. The generic type T represents the data type of the members of the Set or Collection, for example, Parameter for the ParameterSet which can be retrieved from the Element.Parameters property, and FamilyParameter for the FamilyParameterSet from the FamilyManager.Parameters.
The following is a typical usage of the method:
…
List<Parameter> list = RawConvertSetToList<Parameter>(element.Parameters);
…
We have to specify the same Parameter type twice, but that is the way the generic method, its return type, and the generic type List<T> work. Anyway it’s more concise, self explanatory and readable. We do not need to worry about the LINQ grammars anymore.
Here are some more usage examples:
…
FamilyParameter fp = RawConvertSetToList<FamilyParameter>(fm.Parameters).
FirstOrDefault(e => e.Definition.Name.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase));
…
…
FamilyType famType = RawConvertSetToList<FamilyType>(fm.Types).
FirstOrDefault(e => e.Name.Equals(familyTypeName, StringComparison.CurrentCultureIgnoreCase));
…
Besides these examples, the technique actually can be applied to all Set or Collection classes. In terms of Revit API ones, a few more are listed below:
ElementSet
CitySet
CategorySet
ConnectorSet
DimensionTypeSet
ViewSet
WallTypeSet
RevitAddinWizard uses this technique in its various Parameter related coders.
Recent Comments