无法将属性或索引器“AnonymousType#1.FilePath”分配给

时间:2013-08-01 20:50:10

标签: entity-framework c#-4.0

基本上我从表中检索了记录,并希望更新一列。

var query = cdrContext.tabless.Where(c => c.FacilityID == facilityID && c.FilePath != null && c.TimeStationOffHook < oldDate)
                                .OrderBy(c => c.TimeStationOffHook)
                                .Skip(size)
                                .Take(pageSize)
                                .Select(c => new { c.FilePath, c.FileName })
                                .ToList();

所以这个查询只有两个字段:FilePath和FileName,然后我要分配FilePath = null;

foreach (var y in query)
{
     y.FilePath = null;
}
cdrContext.SaveChanges();

然后我收到了一个错误:

Property or indexer 'AnonymousType#1.FilePath' cannot be assigned to -- it is read only 

1 个答案:

答案 0 :(得分:0)

您可以从查询匿名类中进行选择。您无法设置匿名类的属性。要做你想做的事,你应该得到整个实体:

var query = cdrContext.tabless.Where(c => c.FacilityID == facilityID && c.FilePath != null && c.TimeStationOffHook < oldDate)
                                .OrderBy(c => c.TimeStationOffHook)
                                .Skip(size)
                                .Take(pageSize)
                                .ToList();

foreach (var y in query)
{
     y.FilePath = null;
}
cdrContext.SaveChanges();