Hi @Noah Aas , Welcome to Microsoft Q&A,
In your LINQ query, you are trying to use the Where
method, but the Where
method expects a condition that returns a Boolean value. In your code, you pass a condition, but it needs to return a boolean, not a list. That's why you're getting a syntax error.
You can use the Any
method to check whether there is an element in a list that meets a specific condition.
var panelID = CurrentOrder.ListSides.SelectMany(side => side.listBoardBikes)
.SelectMany(bike => bike.ListPosition)
.Where(pos => pos.PanelEdge == 0)
.Select(pos => pos.SerialToWrite)
.FirstOrDefault();
This returns the first SerialToWrite property value that satisfies the condition, or null if no element is found that satisfies the condition.
Regarding your second question, there is nothing wrong with your approach. As shown below, when the panelID is found, the found variable will be set to true and the outer loop will be exited at the beginning of the next iteration.
bool found = false;
foreach (var side in Enum.GetValues(typeof(Side)).Cast<Side>())
{
if (found)
break;
var listBoardBikes = CurrentOrder.ListSides[(int)side - 1].listBoardBikes.OrderBy(x => x.PanelIndex).ToList();
foreach (var bike in listBoardBikes)
{
if (found)
break;
foreach (var pos in bike.ListPosition)
{
if (pos.PanelEdge == 0)
{
panelID = pos.SerialToWrite;
found = true;
break;
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.