Search code examples
asp.net-mvcdatabaselinqicollection

LINQ related error "Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context


I have some code that is selecting some values from my database. I have a IQueryable of requests, that contains a ICollection of rooms.

I want to get a List<ICollection<rooms>>. I have tried the following and i recieve the above error.

Any ideas??

public ActionResult _roomChecker(checkRooms JSONdata){
        var rooms = db.rooms.Include(r=>r.building).Include(r=>r.facilities);
        rooms = rooms.Where(r => r.capacity >= JSONdata.capacity);
        if (JSONdata.type != "Any")
        {
            rooms = rooms.Where(r => r.roomType.Equals(JSONdata.type));
        }
        if (JSONdata.park != "Any")
        {
            rooms = rooms.Where(r => r.building.park.Equals(JSONdata.park));
        }

        if (JSONdata.facilities != null)
        {
            for (var i = 0; i < JSONdata.facilities.Length; i++)
            {
                rooms = rooms.Where(r => r.facilities.Any(f => f.facilityName.Equals(JSONdata.facilities[i])));

            }
        }

        var proposedRequest = db.requests.Include(r => r.rooms);
        proposedRequest = proposedRequest.Where(r=>r.booked.Equals(1));
        proposedRequest = proposedRequest.Where(r => r.roundID.Equals(JSONdata.roundID));
        proposedRequest = proposedRequest.Where(r => r.day.Equals(JSONdata.day));
        proposedRequest = proposedRequest.Where(s => s.start < JSONdata.start + JSONdata.length && s.start + s.length > JSONdata.start);
        int[] standardWeeks = new int[12] {1,2,3,4,5,6,7,8,9,10,11,12};
        var containsStandard = standardWeeks.Intersect(JSONdata.weeks);
        if (containsStandard.Count()!=0)
        {
            proposedRequest = proposedRequest.Where(r => r.weeks_request.Any(f => JSONdata.weeks.Contains(f.week)) || r.weeks.Equals(1));
        }
        else {
            proposedRequest = proposedRequest.Where(r => r.weeks_request.Any(f => JSONdata.weeks.Contains(f.week)));
        }

    //ERROR OCCURS ON THIS LINE BELOW

        List<ICollection<room>> bookedRooms = proposedRequest.Select(r => r.rooms).ToList();


        var deptRooms = db.rooms.Include(r => r.building).Include(r => r.facilities).Where(r => r.belongsTo.Equals(JSONdata.deptCode));

        roomCheckerObject suitableRooms = new roomCheckerObject();
        suitableRooms.code = JSONdata.deptCode;
        suitableRooms.roomNo = JSONdata.roomNo;
        suitableRooms.RequestNo = JSONdata.RequestNo;
        if(rooms.Count() >0){
            suitableRooms.rooms = rooms.ToList();
            var buildings = rooms.Select(r => r.building).Distinct();
            suitableRooms.buildings = buildings.ToList();
        }
        if(bookedRooms.Count() >0){
            suitableRooms.bookedRooms = bookedRooms;
        }

        if(deptRooms.Count() >0){
            suitableRooms.deptRooms = deptRooms.ToList();
        }
        return PartialView(suitableRooms);}

Solution

  • It's kind of difficult to say what exactly is causing the error but I can see 3 possible culprits:

    1) You're using .Equals() with a Linq to entities (L2E) query. This can cause problems, see here.

    2) You say the error occurs on the line where List<ICollection<room>> is declared. On this line you call .ToList() for the first time in any of L2E statements. Only now will your L2E query be executed (see EF Query Execution) against the database meaning possibly any error (using .Equals()) in a previous L2E statement could throw the exception.

    3) Does proposedRequest.Select(r => r.rooms).ToList(); really return an List<ICollection<room>>? Try using .SelectMany() (though this may have undesirable effects) or for a sanity check change List<ICollection<room>> to var to see what the query will return.