Extract data from complex dictionary

  • Thread starter Thread starter Luigi Z
  • Start date Start date
L

Luigi Z

Hi all,
having a dictionary like this (C# 2.0)

private IDictionary mapVoceSourceCodiciToVoci = new
Dictionary<CheckPairBase, VoceSource[]>();

where CheckPairBase and VoceSource are classes, and is populated in this way:

mapVoceSourceCodiciToVoci.Add(new
CheckPairBase(Attivita.TavolaCodice, Attivita.A11_TitoliDiStato, new
Column(Attivita.Column1)),
new VoceSource[] { new
VoceSource("311100", null, null, VoceSource.Operator.Plus),
new VoceSource("363100", null, null,
VoceSource.Operator.Plus), new VoceSource("365100", null, null,
VoceSource.Operator.Plus),
new VoceSource("463100", null, null,
VoceSource.Operator.Plus), new VoceSource("465100", null, null,
VoceSource.Operator.Plus) });

.......etc

how can I extract a List<string> of every code (like "123456") present as
property in VoceSource class?

Thanks in advance.
 
Luigi said:
Hi all,
having a dictionary like this (C# 2.0)

private IDictionary mapVoceSourceCodiciToVoci = new
Dictionary<CheckPairBase, VoceSource[]>();

where CheckPairBase and VoceSource are classes, and is populated in this way:

mapVoceSourceCodiciToVoci.Add(new
CheckPairBase(Attivita.TavolaCodice, Attivita.A11_TitoliDiStato, new
Column(Attivita.Column1)),
new VoceSource[] { new
VoceSource("311100", null, null, VoceSource.Operator.Plus),
new VoceSource("363100", null, null,
VoceSource.Operator.Plus), new VoceSource("365100", null, null,
VoceSource.Operator.Plus),
new VoceSource("463100", null, null,
VoceSource.Operator.Plus), new VoceSource("465100", null, null,
VoceSource.Operator.Plus) });

......etc

how can I extract a List<string> of every code (like "123456") present as
property in VoceSource class?

Thanks in advance.

Something like:

List<string> codes = new List<string>();
foreach (VoceSource[] sources in mapVoceSourceCodiciToVoci.Values) {
codes.AddRange(sources);
}
 
Göran Andersson said:
Something like:

List<string> codes = new List<string>();
foreach (VoceSource[] sources in mapVoceSourceCodiciToVoci.Values) {
codes.AddRange(sources);
}

Thank you Goran, but it gives me this error:

Unable to cast object of type 'Bnpparibas.RFA.Model.Entities.VoceSource[]'
to type 'System.Collections.Generic.IEnumerable`1[System.String]'.

Luigi
 
Göran Andersson said:
Something like:

List<string> codes = new List<string>();
foreach (VoceSource[] sources in mapVoceSourceCodiciToVoci.Values) {
codes.AddRange(sources);
}

Thank you Goran, but it gives me this error:

Unable to cast object of type 'Bnpparibas.RFA.Model.Entities.VoceSource[]'
to type 'System.Collections.Generic.IEnumerable`1[System.String]'.

That's correct. VoceSource[] is an array of VoceSource objects, not strings,
and can't be directly converted to strings. I can only assume Goran didn't
read your (very busy) code sample closely and just gave some air code.

You have little choice but to add a second loop under the foreach above and
add the strings to the list one-by-one. In other words, the answer to your
original question is "Loops." If you were hoping for a magical,
do-it-for-you-automatically way, there isn't one.
 
Luigi said:
Göran Andersson said:
Something like:

List<string> codes = new List<string>();
foreach (VoceSource[] sources in mapVoceSourceCodiciToVoci.Values) {
codes.AddRange(sources);
}

Thank you Goran, but it gives me this error:

Unable to cast object of type 'Bnpparibas.RFA.Model.Entities.VoceSource[]'
to type 'System.Collections.Generic.IEnumerable`1[System.String]'.

Luigi

Right, you need to loop through the array and get each property. I don't
know what you called your property, in the code below I assume that it's
called Code:

List<string> codes = new List<string>();
foreach (VoceSource[] sources in mapVoceSourceCodiciToVoci.Values) {
foreach (VoceSource source in sources) {
codes.Add(source.Code);
}
}
 
Back
Top