排序动态列表是只读的吗?

时间:2013-03-12 12:50:20

标签: c# asp.net linq

我有一个动态列表,我尝试排序,然后根据新的排序顺序更改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?

由于

1 个答案:

答案 0 :(得分: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))
                        }
                   );