I have folowing list that but i want distinct rows on the basis of catID how can i achieve this?
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;
MoreLINQ和DistinctBy优于所有GroupBy黑客:
return lst.DistinctBy(x => x.CatID);
GroupBy
使用CatID
属性并返回每个组的First
元素:
return lst.GroupBy(x => x.CatID).Select(g => g.First()).ToList();
You can do this:
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);
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.