4

我有一个动态列表,我尝试对其进行排序,然后根据新的排序顺序更改 id:

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();
}

但是它在 sortedList.ElementAt(i).id = i.ToString();声明它是只读的时候崩溃了?

不能将属性或索引器<>f__AnonymousType4<string, string,string,string,string,string,bool,string,bool,string>.id分配给——它是只读的

如何更改 ID?

谢谢

4

1 回答 1

5

如前所述,您无法更新匿名类型,但是您可以修改您的流程以使用一个查询,该查询首先对项目进行排序,并将项目的索引作为参数包含在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))
                        }
                   );
于 2013-03-12T14:00:24.303 回答