gpt4 book ai didi

c# - 排序的动态列表是只读的?

转载 作者:太空狗 更新时间:2023-10-30 01:06:30 29 4
gpt4 key购买 nike

我有一个动态列表,我尝试对其进行排序,然后根据新的排序顺序更改 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(); 上崩溃了说明它是只读的?

Property or indexer <>f__AnonymousType4<string, string,string,string,string,string,bool,string,bool,string>.id cannot be assigned to -- it is read only

如何更改 ID?

谢谢

最佳答案

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

关于c# - 排序的动态列表是只读的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15361715/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com