实体框架 - 在非持久化类上创建IQueryable

时间:2016-04-15 00:26:16

标签: c# entity-framework linq

我有一个OData实体框架服务(EF5)。我有一个案例,我有一组数据库记录,需要在返回之前进行操作。

具体而言,这是树结构中的记录列表,该结构的信息位于数据库中。我返回的数据集必须被展平 - 所以我需要在内存中创建树,然后在处理后返回一个新的数据集(按正确的顺序放置所有内容并进行格式化)。虽然我显然需要从db获取整个集合(通常介于50到400个记录之间,所以没什么大不了的),但我希望保持IQueryable能力按需发送子集。

我查看了以下Create LINQ to entities OrderBy expression on the fly - 这似乎表明它是可能的。

我创建了一个新服务来执行此操作,但在启动服务时会引发内部错误。关于是否可以做到这一点的任何建议,如果有的话,将非常感激。

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

0 个答案:

没有答案