I have the following enums and class:
public enum SymbolTypeEnum
{
[Description("Forex")]
forex = 0,
[Description("Metals")]
metals = 1,
[Description("Commodities")]
commodities = 2,
[Description("Indices")]
indices = 3,
[Description("Cryptocurrency")]
cryptocurrency = 4,
[Description("Other")]
other = 5
}
public enum SymbolClassificationEnum
{
[Description("None")]
none = 0,
[Description("Major")]
major = 1,
[Description("Minor")]
minor = 2,
[Description("Exotic")]
exotic = 3
}
public class SymbolSettingsModel
{
[PrimaryKeyAttribute, Unique]
public string symbolName { get; set; }
public SymbolTypeEnum symbolType { get; set; }
public SymbolClassificationEnum symbolClassification { get; set; }
}
I have a List<SymbolSettingsModel> symbolSettings;
My goal is to generate a Dictionary<SymbolTypeEnum, List<SymbolDescr>> typeSymbolsSettingsData;
Where SymbolDescr is:
class SymbolDescr
{
public string symbol { get; set; }
public int classification { get; set; }
}
The idea is to group them by symbolType and use it as a Key for the dictionary, and generate a list of SymbolDescr. Here is my code so far:
typeSymbolsSettingsData = symbolSettings.GroupBy(p => p.symbolType)
.ToDictionary(p => p.Key, p => p.ToList());
I am stuck at this point, do you have any ideas how I can do this ?
CodePudding user response:
You could use Select
to project your SymbolSettingsModel
instances into SymbolDescr
instances:
typeSymbolsSettingsData = symbolSettings
.GroupBy(p => p.symbolType)
.ToDictionary(
p => p.Key,
p => p.Select(x => new SymbolDescr
{
symbol = x.symbolName,
classification = (int)x.symbolClassification
})
.ToList());
You should also try to comply with the .Net property naming convention which mandates the use of PascalCase.