2

次のリストがありますが、catID に基づいて個別の行が必要です。どうすればこれを達成できますか?

lst.AddRange(
            (from xx in this.FreeInstructionItems
             select new selectedCustomization()
             {
                 TypeName = CategoryType.SpecialInstruction,
                 CategoryName = xx.InstructionInfo.CatName,
                 ItemName = xx.InstructionInfo.Description,
                 SourceID = xx.InstructionInfo.InstructionId,
                 CatID = xx.InstructionInfo.CatID,
                 Items = GetAllFreeItemNames(CategoryType.SpecialInstruction, xx.InstructionInfo.CatID)
             }
            ).ToList()
            );
return lst;
4

4 に答える 4

2

MoreLINQと DistinctBy は、すべての GroupBy ハックより優れています。

return lst.DistinctBy(x => x.CatID);
于 2013-08-05T10:45:14.313 に答える
1

あなたはこれを行うことができます:

var results = 
    (from xx in this.FreeInstructionItems
     group xx by xx.InstructionInfo.CatID into g
     let instrInfo = g.First().InstructionInfo
     select new selectedCustomization()
     {
         TypeName = CategoryType.SpecialInstruction,
         CategoryName = instrInfo.CatName,
         ItemName = instrInfo.Description,
         SourceID = instrInfo.InstructionId,
         CatID = instrInfo.CatID,
         Items = GetAllFreeItemNames(
             CategoryType.SpecialInstruction, 
             instrInfo.CatID)
     });
lst.AddRange(results);
于 2013-08-05T10:41:26.827 に答える
1

GroupByすべてのグループのCatIDプロパティと戻り要素を使用する:First

return lst.GroupBy(x => x.CatID).Select(g => g.First()).ToList();
于 2013-08-05T10:40:59.447 に答える
1
lst.AddRange(this.FreeInstructionItems.GroupBy(x=>x.InstructionInfo.CatID)
                                      .FirstOrDefault()
                                      .SelectMany(x=> new selectedCustomization()
              {
               TypeName = CategoryType.SpecialInstruction,
               CategoryName = x.InstructionInfo.CatName,
               ItemName = x.InstructionInfo.Description,
               SourceID = x.InstructionInfo.InstructionId,
               CatID = x.InstructionInfo.CatID,
               Items = GetAllFreeItemNames(CategoryType.SpecialInstruction, xx.InstructionInfo.CatID)
              }).ToList());
于 2013-08-05T10:42:38.263 に答える