question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked ·

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

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-wpfdotnet-csharp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered ·

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 ) ); 


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.