开发者

A LINQ Challenge

I have a struct like this:

 public struct MyStruct
 {
     public string Name;
     public bool Process;
 }

And I have a list of myStruct like this:

"123", true

"123", false

"234", true

"345", false

"456", true

"456", false

I want to able to use LINQ to return a list like this:

"123", false

"234", true

"345", false

"456", false

So basically the result I want is a list of distinct names ("123", "234", ...etc) along with the boolean flag and if the names are repeated I need to do an "AND" operation on the flag.

Is there an easy way to do this with one s开发者_如何转开发ingle LINQ statement?


var result = input.GroupBy(e => e.Name)
                  .Select(gr => new { Name = gr.Key,
                                      All = gr.All(e => e.Process) });


public struct MyStruct
{
     public string Name;
     public bool Process;
}    

public void LinqCellenge()
{
   var sourceList = Enumerable.Empty<MyStruct>();

    var resultList = sourceList
      .GroupBy(item => item.Name, (name, values) => new MyStruct()
        {
          Name = name,
          Flag = values.All(x => x.Flag)
        });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜