LinQ, GroupBy, C#, sub list, check state - improvement of the query

Markus Freitag 3,786 Reputation points
2020-11-26T08:25:37.973+00:00

Hello,

The below code works very well.
The question is, can I solve this in one query?
If so, how would I implement it?

I want to know if a position has a bad status. If so, the product is bad. Return value is bool.

Thanks for tips in advance!

var qryPanelIndex = CurrentProduct.MainList[0].ListSingleBoards.GroupBy(info => info.PanelIndex)
        .Select(group => new
        {
            PanelIndex = group.Key,
            CountOccurrences = group.Count(),
            PanelListPos = group.ToArray() //.ToList().Where(pos => pos.ListPosition.Any(cState => cState.State.HasFlag(StatePosition.ProcessWrong) == true)).FirstOrDefault()
        })
        .OrderBy(x => x.PanelIndex).ToList();

bool iscState = false;
foreach(var item in qryPanelIndex)
{
    foreach( var subitem in item.PanelListPos)
    {
        iscState = subitem.ListPosition.Any(st => st.State.HasFlag(StatePosition.ProcessWrong));
        if (iscState)
            break;
    }
    if (iscState)
        break;
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,208 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2020-11-26T12:14:42.097+00:00

    If you do not need the qryPanelIndex list for other purposes, then try something like this:

    bool iscState = CurrentProduct.MainList[0].ListSingleBoards.Any( b => b.ListPosition.Any( p => p.State.HasFlag( StatePosition.ProcessWrong ) ); 
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful