0

我正在开发一个将数据返回到 Windows Phone 应用程序的 REST 服务,并且查询的响应时间是一个考虑因素。目前的代码如下所示:

List<RestModels.Response.RealWorkOrder> RWOList = _context.MOB_WORKORDERS_SUMMARY
                .Where(mws => _context.TBL_USER_GROUP_MEMBERS.Any(u => u.user_Id == UserID && u.user_Group_Id == mws.user_group_id))// && !disallowedStatuses.Contains(mws.workorder_status_id ?? -1))
                .Select(mob => _context.FMIS_WORKORDER.FirstOrDefault(wo => wo.workorder_id == mob.workorder_id))
                .Select(wo =>
            new RestModels.Response.RealWorkOrder()
            {
                AssetLocation = wo.FMIS_ASSET_LOCATION.asset_location_description,
                IAssets = wo.FMIS_WORKORDER_ASSETS.Select(woa => new RestModels.Subclasses.Asset()
                {
                    AssetId = woa.FMIS_ASSET.asset_id,
                    AssetName = woa.FMIS_ASSET.asset_title + " - " + woa.FMIS_ASSET.asset_description,
                    SerialNumber = woa.FMIS_ASSET.asset_UF_1
                }),
                ...

                CategoryDescription = _context.VIEW_WORK_CATEGORIES.Where(cat => cat.work_category_id == wo.FMIS_WORKREQUEST1.FirstOrDefault().workrequest_category).FirstOrDefault().work_category_title + " - " +
                    _context.VIEW_WORK_CATEGORIES.Where(cat => cat.work_category_id == wo.FMIS_WORKREQUEST1.FirstOrDefault().workrequest_category).FirstOrDefault().work_category_description + " [" +
                    _context.VIEW_WORK_CATEGORIES.Where(cat => cat.work_category_id == wo.FMIS_WORKREQUEST1.FirstOrDefault().workrequest_category).FirstOrDefault().work_category_priority_name + "]",

我的问题与 CategoryDe​​scription 字段有关——事实证明,连接非常昂贵。有没有办法对视图记录进行单一引用并即时连接字段?

4

1 回答 1

1

你能试试这个吗,我避免了多次数据库调用。我还没有编译它,所以可能存在一些语法问题。

List<RestModels.Response.RealWorkOrder> RWOList = _context.MOB_WORKORDERS_SUMMARY
            .Where(mws => _context.TBL_USER_GROUP_MEMBERS.Any(u => u.user_Id == UserID && u.user_Group_Id == mws.user_group_id))// && !disallowedStatuses.Contains(mws.workorder_status_id ?? -1))
            .Select(mob => _context.FMIS_WORKORDER.FirstOrDefault(wo => wo.workorder_id == mob.workorder_id))
             //// added below line to get the category item in one go
            .Select(tempLet => new {workOrder = tempLet, CategoryItem = _context.VIEW_WORK_CATEGORIES.Where(cat => cat.work_category_id == tempLet.FMIS_WORKREQUEST1.FirstOrDefault().workrequest_category).FirstOrDefault()})
            .Select(wo =>
        new RestModels.Response.RealWorkOrder()
        {
            AssetLocation = wo.workOrder.FMIS_ASSET_LOCATION.asset_location_description,
            IAssets = wo.workOrder.FMIS_WORKORDER_ASSETS.Select(woa => new RestModels.Subclasses.Asset()
            {
                AssetId = woa.FMIS_ASSET.asset_id,
                AssetName = woa.FMIS_ASSET.asset_title + " - " + woa.FMIS_ASSET.asset_description,
                SerialNumber = woa.FMIS_ASSET.asset_UF_1
            }),
            ...

            CategoryDescription = wo.CategoryItem.work_category_title + " - " +
                wo.CategoryItem.work_category_description + " [" +
                wo.CategoryItem.work_category_priority_name + "]",
于 2015-08-20T13:51:46.893 回答