We will attempt to do something which is seldom done or discussed in Tekla code samples: and that is the very important issue of refactoring code.
1. Start with Tests.
The first thing you need is a good suite of tests. That way you will know whether something has gone wrong or not. It might be tricky doing this since we are developing within the Tekla environment, but I do suppose it’s possible.
2. General Themes:
- Change names to more appropriate titles.
- Restructure the code to make it understandable.
- Correct any obvious defects:
when I try to use this method
private List groupBeamsByPartNameAndFinish(PartSelector beams)
{
return beams.GroupBy(part => new NameProfileFinish() { Name = part.Name, Profile = part.Profile.ProfileString, Finish = part.Finish.ToString() }, (key, group) => new AggregateBeam { NPF = key, Beams = group.DistinctBy(b => b.Identifier).ToList() }).ToList();
}
I get this error:
Error CS1061 ‘IEnumerable’ does not contain a definition for ‘DistinctBy’ and no accessible extension method ‘DistinctBy’ accepting a first argument of type ‘IEnumerable’ could be found (are you missing a using directive or an assembly reference?)
DistinctBy is taken from a library – MoreLinq: https://github.com/morelinq/MoreLINQ. You can download this package from Visual Studio using the Nuget Package manager.
See the following: https://www.nuget.org/packages/morelinq/
Don’t forget to add in the using statement at the top of your file.