You can also consider this method, merge them, and then group them to determine whether there is false in each group:
public static void Main()
{
List<A> list1 = new List<A>() {
new A(){ Index = 1, State = false },
new A(){ Index = 2, State = false },
new A(){ Index = 3, State = true },
new A(){ Index = 4, State = false },
new A(){ Index = 5, State = false },
};
List<A> list2 = new List<A>() {
new A(){ Index = 1, State = false },
new A(){ Index = 2, State = true },
new A(){ Index = 3, State = true },
new A(){ Index = 4, State = false },
new A(){ Index = 5, State = false },
new A(){ Index = 6, State = true },
new A(){ Index = 7, State = false },
};
var list = list1.Concat(list2);
var re = from a in list
group a by a.Index into g
select new
{
Key = g.Key,
Value = g.Where(x => x.State == false).Count() > 0 ? false : true
};
Dictionary<int, bool> dict1 = re.ToDictionary(x => x.Key, y => y.Value);
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
@Markus Freitag
The code can be written like this:
var re1 = list.GroupBy(a => a.Index).Select((g, index) => new { g.Key, Value = g.Where(x => x.State == false).Count() > 0 ? false : true });
I noticed that you modified Value = xxx in the code. Now as long as there is true in the group, the corresponding Value in the result is true, which seems to be contrary to your original description.
Did the requirements change, or was it accidentally wrong?
By the way, I don't know how to add a breakpoint in that website, I haven't used it.
I wanted to test in case the requirement of the check should be changed. So I have all possibilities. Thanks a lot.