1

I have an OData Entity Framework service (EF5). I have a case where I have a set of database records that need to be manipulated prior to returning them.

Specifically, this is a list of records that are in a tree structure, and the information for that structure is in the database. The dataset I am returning has to be flattened - so I need to create the tree in memory, and then return a new data set after processing (putting everything in the right order and formatting). While I obviously need to get the whole set from the db (typically between 50 and 400 records so no big deal), I want to maintain the IQueryable capability to send a subset on demand.

I looked at the following Create LINQ to entities OrderBy expression on the fly - which seems to suggest it is possible.

I have created a new service to do this, but it throws an internal error when the service is started. Any advice on whether this can be done and if so how - would be much appreciated.

    [WebGet]
    public IQueryable<mobFlatSchedule> getSchedule(int projectID)
    {
        //get the records from the context
        List<ScheduleItem> siList = CurrentDataSource.ScheduleItems.Where(x => x.ProjectID == projectID).ToList();
        //convert the tree into a flat list of basic types
        List<mobFlatSchedule> flatSIList = scheduleFlattener.CreateScheduleDatasource(siList);
        return flatSIList.AsQueryable();
    }

    public class mobFlatSchedule
    {
        public int Level { get; set; }
        public int lineType { get; set; }
        public decimal? Qty { get; set; }
        public decimal? QtySched { get; set; }
        public decimal? Rate { get; set; }
        public int ScheduleID { get; set; }
        public int ProjectID { get; set; }
        public string ScheduleNo { get; set; }
        public string ScheduleDescription { get; set; }
        public decimal? SchedTotal { get; set; }
        public decimal? Total { get; set; }
        public string Unit { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="mobFlatSchedule"/> class.
        /// </summary>
        public mobFlatSchedule(CivilPro.InfrastructureC.Schedule.FlatSchedule fs)
        {
            Level = fs.Level;
            lineType = (int) fs.lineType;
            Qty = fs.Qty1;
            QtySched = fs.QtySched;
            Rate = fs.Rate1;
            ScheduleID = fs.Schedule.ScheduleID;
            ProjectID = fs.Schedule.ProjectID.ProjectID;
            ScheduleNo = fs.ScheduleNo;
            ScheduleDescription = fs.ScheduleDescription;
            SchedTotal = fs.SchedTotal;
            Total = fs.Total1;
            Unit = fs.Unit;
        }
    }
Community
  • 1
  • 1
statler
  • 1,322
  • 2
  • 15
  • 24
  • Include `CreateScheduleDatasource` please, also you're executing the query by calling `ToList()`, calling `AsQueryable()` later on merely casts it back to an `IQueryable`. – Alexander Derck Apr 15 '16 at 06:57
  • It is deliberate calling the tolist. This is to get the query executed. All the data is necessary to compile the tree structure. This is then processed to a different class, really how that is done is irrelevant. It is cast to an iqueryable because I was hoping it might enable an auto implement the take, skip etc (seems I can't) – statler Apr 21 '16 at 10:28

0 Answers0