I'm working on a REST Service that returns data to a Windows Phone App and the response time of the query is a consideration. The code currently looks like this:
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 + "]",
My question relates to the CategoryDescription field - the concatenation is proving to be quite expensive. Is there a way to make a single reference to the view record and concatenate the fields on the fly?
Can you try this, I have avoided multiple database calls. I haven't compiled it so there could be some syntax issues.
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 + "]",