1

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

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 
4

1 回答 1

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();
于 2013-08-01T20:54:22.493 回答