我需要重构此代码,以便数据服务不会每行项查询两个workType.提前致谢.
_attributeGroups = attributeGroups.Select(attributeGroupRowModel =>
new AttributeGroupRowModel()
{
Name = attributeGroupRowModel.Name,
WorkType = workTypes.First(wt => wt.Id == attributeGroupRowModel.WorkTypeId).Description,
IsExpired = workTypes.First(wt => wt.Id == attributeGroupRowModel.WorkTypeId).IsExpired, //todo: not efficient, to be refactored
}).ToList();
Run Code Online (Sandbox Code Playgroud)
_attributeGroups = attributeGroups.Select(attributeGroupRowModel =>
{
var wt = workTypes.First(x => x.Id == attributeGroupRowModel.WorkTypeId);
return new AttributeGroupRowModel()
{
Name = attributeGroupRowModel.Name,
WorkType = wt.Description,
IsExpired = wt.IsExpired,
};
}).ToList();
Run Code Online (Sandbox Code Playgroud)
或者如果您更喜欢LINQ:
_attributeGroups =
(from attributeGroupRowModel in attributeGroups
let wt = workTypes.First(x => x.Id == attributeGroupRowModel.WorkTypeId)
select new AttributeGroupRowModel()
{
Name = attributeGroupRowModel.Name,
WorkType = wt.Description,
IsExpired = wt.IsExpired,
}).ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |