I have a collection of type Collection<Lookup<int>> and I would like to convert its values to Collection\Lookup<int?>>. What's the best way to do this?
Thank you
Well, a Lookup is a collection of keyed IEnumerables; basically a read-only Dictionary<TKey, IEnumerable<TValue>>. A Lookup<int> is nonsensical.
Probably the best way to tackle this would be to "de-group" each item from the Lookups into an anonymous key-value pair with a nullable value, then re-group the items.
Example:
var myCollectionOfNullableInts =
(from g in MyCollectionOfIntLookups
from v in g.Values
select new {g.Key, Value = (int?)v}
into l
group l by l.Key into g2
select g2).ToList();
The resulting collection SHOULD be a List<Lookup<[your key type], int?>>.
System.Collection.ObjectModel.Collection<T> is related to IEnumerable<T> you should be able to use the Select extension method.
Something along the lines of:
var listOfNullables = lookupsAsInt.Select(l => new Lookup<Int?>(l.Value)).ToList();
You'll need to include using System.Linq; in your class file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With