I am using the following Linq-to-XML to load some XML structures into my data structures.
// Load all the definitions
var definitions = doc.Descendants(Constants.ScriptNode)
.Select(x => new TcScriptDefinition
{
Application = x.Attribute(Constants.AppAttribute).Value,
CaseName = x.Attribute(Constants.CaseAttribute).Value,
ActionType = x.Attribute(Constants.ActionAttribute).Value,
ScriptUnit = x.Attribute(Constants.UnitAttribute).Value,
ScriptMethod = x.Attribute(Constants.MethodAttribute).Value,
Parameters = x.Descendants(Constants.ParamNode)
.Select(param => new TcScriptParameter
{
Code = param.Attribute(Constants.ParamCodeAttribute).Value,
ParameterNumber = Convert.ToInt32(param.Attribute(Constants.ParamOrderAttribute).Value),
DisplayString = param.Attribute(Constants.ParamDisplayAttribute).Value
})
.ToList()
})
.ToList();
The problem is that the TcScriptDefinition.Parameters is defined as a HashSet<TcScriptParameter> and thus the ToList() fails to compile since it returns a List<T>.
How can I load my xml into a HashSet<T> via Linq?
As an alternative to creating an extension method for ToHashSet, you can also just construct the
HashSet<T>on the fly, by changing the relevant section to: