3

データ サービスが行項目ごとに 2 つの 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();
4

2 に答える 2

6
_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();

または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();
于 2012-09-25T05:16:21.357 に答える
4

式ラムダの代わりにステートメント ラムダを使用できます。ステートメント lambda を使用すると、とりわけ、変数を定義できます。

_attributeGroups = attributeGroups.Select(attributeGroupRowModel => 
{
    var w = workTypes.First(wt => wt.Id == attributeGroupRowModel.WorkTypeId);
    return new AttributeGroupRowModel()
    {
        Name = attributeGroupRowModel.Name,               
        WorkType = w.Description,
        IsExpired = w.IsExpired,
    };
}).ToList();
于 2012-09-25T05:16:14.353 に答える