2 回答
TA贡献1818条经验 获得超8个赞
var result = items.OrderBy(x => x.Name) .ThenByDescending(x => x.OddValue) .GroupBy(s => new {s.IdSelectionType, s.Name, s.Visible}) .Select(x => x.FirstOrDefault()) .ToList();
首先对项目排序Name
,OddValue
然后按分组Name
。OddValue != null
在带有 的条目之前,您将拥有带有的条目OddValue == null
。然后取每个分组的第一个条目。
这也将返回OddValue == null
具有相同的条目,而没有其他条目Name
。
最终结果:https ://dotnetfiddle.net/jZ5gs2
TA贡献1834条经验 获得超8个赞
你可以使用联合。选择所有不具有 OddValue == null 的元素,然后将剩余的(具有 null 的元素)与第一个列表合并。
https://linqsamples.com/linq-to-objects/set/Union
var originalList = new[]
{
new Selection() {IdSelectionType = "1", Name = "Under (75.50)", Visible = true},
new Selection() {IdSelectionType = "2", Name = "Under (75.50)", Visible = true},
//new Selection() {IdSelectionType = "1", Name = "Under (75.50)", OddValue = 1.840m, Visible = true},
new Selection() {IdSelectionType = "2", Name = "Under (75.50)", OddValue = 1.860m, Visible = true},
};
var noneNulls = originalList.Where(ol => ol.OddValue != null);
var endList = originalList
.Where(ol => !noneNulls.Select(nn => nn.IdSelectionType).Contains(ol.IdSelectionType))
.Union(noneNulls);
工作示例:https ://dotnetfiddle.net/UJ8FaS
- 2 回答
- 0 关注
- 284 浏览
添加回答
举报