BLToolkit - Operation is not valid due to the current state of the object

493 views Asked by At

We're using BLToolkit library in our ASP.NET MVC 4 application. I get an "Operation is not valid due to the current state of the object" when trying to execute the LINQ statement below:

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;
        }
    }

Models:

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; }
}

The df.LifeCycleYears and ff.ItemCount has a type of ushort so there's an implicit ushort to decimal conversion in the LINQ statement above.

1

There are 1 answers

0
Hao Nguyen On

I think it was not an exception from BltToolkit. Please check the answer from this link: [1]

My source:

[1] 'Operation is not valid due to the current state of the object' error during postback