There are two lists:
List<string> files;
List<Filter> filters;
I want the result to be like:
List<KeyValuePair<string, Filter>> fileFilterMap;
I tried several stuff (lambda expressions, linq) but failed. I really do not want the
for(int i = 0; i< files.count; i++)
method.
You can use:
List<KeyValuePair<string, Filter>> fileFilterMap =
Enumerable.Range(0, files.Count)
.Select(i => new KeyValuePair<string, Filter>(files[i], filters[i]))
.ToList();
Or:
List<KeyValuePair<string, Filter>> fileFilterMap =
Enumerable.Zip(
files,
filters,
(file, filter) => new KeyValuePair<string, Filter>(file, filter)
)
.ToList();
Have a look at Enumerable.Zip.
var fileFilterMap = Enumerable.Zip(files, filters, (file, filter) => new KeyValuePair(file, filter));
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