Linq 多次引用字符串连接中的外键关系

Linq Multiple references to a foreign key relationship in a string concatenation

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

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 字段有关 - 连接被证明是相当昂贵的。有没有办法对视图记录进行单一引用并动态连接字段?

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

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 + "]",