Reputation: 54133
I have a dynamic list that I try to sort then change the id based on the new sort order:
foreach (Case c in cases)
{
bool edit = true;
if (c.IsLocked.HasValue)
edit = !c.IsLocked.Value;
eventList.Add(new {
id = row.ToString(),
realid = "c" + c.CaseID.ToString(),
title = c.CaseTitle + "-" + c.Customer.CustomerDescription,
start = ResolveStartDate(StartDate(c.Schedule.DateFrom.Value.AddSeconds(row))),
end = ResolveEndDate(StartDate(c.Schedule.DateFrom.Value), c.Schedule.Hours.Value),
description = c.CaseDescription,
allDay = false,
resource = c.Schedule.EmployeID.ToString(),
editable = edit,
color = ColorConversion.HexConverter(System.Drawing.Color.FromArgb(c.Color.Value))
});
row++;
}
var sortedList = eventList.OrderBy(p => p.title);
for (int i = 0; i < sortedList.Count(); ++i)
{
sortedList.ElementAt(i).id = i.ToString();
}
But it crashes on sortedList.ElementAt(i).id = i.ToString();
stating that it is read only?
Property or indexer
<>f__AnonymousType4<string, string,string,string,string,string,bool,string,bool,string>.id
cannot be assigned to -- it is read only
How can I change the ids?
Thanks
Upvotes: 4
Views: 273
Reputation: 152596
As mentioned you cannot update an anonymous type, however you can modify your process to use one query that orders the items first and includes the index of the item as a parameter to the Select
:
var query = cases.OrderBy(c => c.CaseTitle + "-" + c.Customer.CustomerDescription)
.Select( (c, i) =>
new {
id = i.ToString(),
realid = "c" + c.CaseID.ToString(),
title = c.CaseTitle + "-" + c.Customer.CustomerDescription,
start = ResolveStartDate(StartDate(c.Schedule.DateFrom.Value.AddSeconds(i))),
end = ResolveEndDate(StartDate(c.Schedule.DateFrom.Value), c.Schedule.Hours.Value),
description = c.CaseDescription,
allDay = false,
resource = c.Schedule.EmployeID.ToString(),
editable = c.IsLocked.HasValue ? !c.IsLocked.Value : true ,
color = ColorConversion.HexConverter(System.Drawing.Color.FromArgb(c.Color.Value))
}
);
Upvotes: 5