Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A LINQ Challenge

Tags:

c#

linq

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 single LINQ statement?

like image 595
notlkk Avatar asked Nov 28 '25 20:11

notlkk


1 Answers

var result = input.GroupBy(e => e.Name)
                  .Select(gr => new { Name = gr.Key,
                                      All = gr.All(e => e.Process) });
like image 81
Timwi Avatar answered Nov 30 '25 11:11

Timwi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!