我们在 ASP.NET MVC 4 应用程序中使用 BLToolkit 库。尝试执行以下 LINQ 语句时,我收到“由于对象的当前状态,操作无效”:
public List<Domain.Entities.Department_Facility> GetByDepartmentId(uint departmentId, uint fiscalYearId)
{
using (var db = new AabViftenDb())
{
BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
var result = (from df in db.department_facilities
where df.Department_ID == departmentId
&& df.departmentfacilityfiscalyearibfk1.Count() > 0
orderby df.ID descending
select new Department_Facility
{
ID = df.ID,
Description = df.Description,
EstablishedYear = df.EstablishedYear,
Facility = new Facility { ID = df.fkDepartmentFacilityfacility1.ID,
Name = df.fkDepartmentFacilityfacility1.Name,
Key= df.fkDepartmentFacilityfacility1.Key
},
LifeCycleYears = df.LifeCycleYears,
FacilityFiscalYear = (from ff in df.departmentfacilityfiscalyearibfk1
where ff.HousingAssociationFiscalYear_ID == fiscalYearId
select new Facility_FiscalYear
{
FiscalYear = new FiscalYears { ID = ff.departmentfacilityfiscalyearibfk2.ID,
FiscalYear = ff.departmentfacilityfiscalyearibfk2.FiscalYear },
ItemCount = ff.ItemCount
}).FirstOrDefault()
}).ToList();
return result;
}
}
楷模:
public class Department_Facility
{
public uint ID { get; set; }
public Department Department { get;set; }
public Facility Facility { get; set; }
public ushort EstablishedYear { get; set; }
public decimal LifeCycleYears { get; set; }
public string Description { get; set; }
public Facility_FiscalYear FacilityFiscalYear { get; set; }
}
public class Facility_FiscalYear
{
public FiscalYears FiscalYear { get; set; }
public decimal ItemCount { get; set; }
}
df.LifeCycleYears 和 ff.ItemCount 具有 ushort 类型,因此在上面的 LINQ 语句中有一个隐式的 ushort 到十进制转换。