Linq GroupBy 并取不为空的条目

Selection 类有 4 个属性:


public class Selection

{

    string IdSelectionType {get; set;}

    string Name {get; set;}

    decimal? OddValue {get; set;}

    bool Visible {get; set;}

}

示例数据


var originalList = new List<Selection>()

        {

            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},

        };

我想选择一个列表,如果有另一个相同但具有 OddValue 的元素!= null,则丢弃具有 OddValue = null 的元素。


是否有任何 Linq / MoreLinq 运算符可以做类似的事情?


Selections.GroupBy(s =>

          new {s.IdSelectionType, s.Name, s.OddValue, s.Visible})

          .Select(g => g.First()).ToList();

http://img1.mukewang.com/61e2b1620001c36704860068.jpg

预期的结果是:

http://img3.mukewang.com/61e2b1680001549e04870029.jpg




哈士奇WWW
浏览 286回答 2
2回答

弑天下

var&nbsp;result&nbsp;=&nbsp;items.OrderBy(x&nbsp;=>&nbsp;x.Name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ThenByDescending(x&nbsp;=>&nbsp;x.OddValue) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(s&nbsp;=>&nbsp;new&nbsp;{s.IdSelectionType,&nbsp;s.Name,&nbsp;s.Visible}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=>&nbsp;x.FirstOrDefault()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList();首先对项目排序Name,OddValue然后按分组Name。OddValue != null在带有 的条目之前,您将拥有带有的条目OddValue == null。然后取每个分组的第一个条目。这也将返回OddValue == null具有相同的条目,而没有其他条目Name。最终结果:https ://dotnetfiddle.net/jZ5gs2

MMMHUHU

你可以使用联合。选择所有不具有 OddValue == null 的元素,然后将剩余的(具有 null 的元素)与第一个列表合并。https://linqsamples.com/linq-to-objects/set/Unionvar originalList = new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Selection() {IdSelectionType = "1", Name = "Under (75.50)", Visible = true},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Selection() {IdSelectionType = "2", Name = "Under (75.50)", Visible = true},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //new Selection() {IdSelectionType = "1", Name = "Under (75.50)", OddValue = 1.840m, Visible = true},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Selection() {IdSelectionType = "2", Name = "Under (75.50)", OddValue = 1.860m, Visible = true},&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var noneNulls = originalList.Where(ol => ol.OddValue != null);&nbsp; &nbsp; &nbsp; &nbsp; var endList = originalList&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(ol => !noneNulls.Select(nn => nn.IdSelectionType).Contains(ol.IdSelectionType))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Union(noneNulls);工作示例:https ://dotnetfiddle.net/UJ8FaS
打开App,查看更多内容
随时随地看视频慕课网APP