I have a immutable List.
In the below g.Select(...) I do a .ToList() which is put into the MemberIdList. I do not want to create a new List with ToList() rather I want to add each r.Field string to the immutable List inside the unit object.
How can I do this?
var groupedCollection = table.AsEnumerable()
.GroupBy(row => new
{
UType = row.Field<string>("UnitType"),
UZoom = row.Field<string>("UnitZoom"),
MZoom = row.Field<string>("MemberZoom"),
});
var unitCollection = groupedCollection
.Select(g => new Unit
{
UnitType = g.Key.UType,
UnitZoom = g.Key.UZoom,
MemberZoom = g.Key.MZoom,
MemberIdList = g.Select(r => r.Field<string>("TestId")).ToList(),
});
List<Unit> units = unitCollection.ToList();
public class Unit
{
public Unit()
{
MemberIdList = new List<String>();
}
public String UnitType { get; set; }
public String UnitZoom { get; set; }
public String MemberZoom { get; set; }
public int MemberOrder { get; set; }
public List<String> MemberIdList { get; private set; }
}
List<T>then? As that definitely has AddRange. You will of course need to tell it the instance, I.e. Obj.MemberIdList.AddRange()List<T>is never immutable. A list-property without a setter is very different to an immutable list.