I'm using an interface IloadPattern
on deriving 3 classes from it LinearLoadingPattern
, JointsLoadingPattern
, AreaLoadingPattern
. At some point, I'm creating a list all the objects created from those classes.
At a later point, I want to retrieve list of a particular object (JointsLoadingPattern
) from the above list, I'm wondering if there is a better way to do this.
Below is the way I'm doing it.
public List<JointsLoadingPattern> GetJointLoads()
{
List<JointsLoadingPattern> jointLoads = new List<JointsLoadingPattern>();
foreach(ILoadPattern loadCase in AllLoadCases)
{
if(loadCase.GetType() ==typeof(JointsLoadingPattern))
{
jointLoads.Add((JointsLoadingPattern)loadCase);
}
}
return jointLoads;
}
AllLoadCases.Where(case => case.GetType() == typeof(JointsLoadingPattern)).ToList()
- i like the LINQ way because it is less verbose. but is exactly the same as what you are already doing right now. \$\endgroup\$OfType
method:AllLoadCases.OfType<JointsLoadingPattern>().ToList()
. \$\endgroup\$