I am trying to concate List<>
as follows-
List<Student> finalList = new List<Student>();
var sortedDict = dictOfList.OrderBy(k => k.Key);
foreach (KeyValuePair<int, List<Student>> entry in sortedDict) {
List<Student> ListFromDict = (List<Student>)entry.Value;
finalList.Concat(ListFromDict);
}
But no concatenation happens. finalList remains empty. Any help?
A call to
Concat
does not modify the original list, instead it returns a new list - or to be totally accurate: it returns anIEnumerable<string>
that will produce the contents of both lists concatenated, without modifying either of them.You probably want to use
AddRange
which does what you want:Or even shorter (in one line of code):
And because
entry.Value
is already of typeList<Student>
, you can use just this: