LinQ query 1:n and 1:n relation

Noah Aas 140 Reputation points
2024-04-22T14:13:35.8+00:00

I have two pages and a list of bicycles. I want to check and query properties. If I do it in detail, it works well.

List<Bike> listBoardBikes;
for (side = Side.Side1; side <= Side.Side2; side++)   // Side 1 and Side 2
{
	listBoardBikes = CurrentOrder.ListSides[(int)side - 1].listBoardBikes.OrderBy(x => x.PanelIndex).ToList();
	foreach (var bike in listBoardBikes)
	{
		foreach (var pos in bike.ListPosition)
		{
			if (pos.PanelEdge == 0)
			{
				panelID = pos.SerialToWrite;
				break;
			}
		}
		if (panelID != "")
			break;
	}
	if (panelID != "")
		break;
}

When I create a query with LinQ I always get syntax errors.

//var qryTest = CurrentOrder.ListSides[(int)side - 1].listBoardBikes.ToList().Where(x => x.ListPosition.Where(y => y.PanelEdge == 0));                

    //.Where(x => x.ListPosition.Where(y => y.PanelEdge == 0));  // this is red from compiler.

enter image description here

What can be the cause. Maybe someone sees it. Thanks in advance.

Last question. If I find what I'm looking for in the bottom list, is there a quick way to get out of the other lists?

         if (panelID != "")
			break;
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,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 33,686 Reputation points Microsoft Vendor
    2024-04-23T03:04:12.1166667+00:00

    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.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2024-04-22T15:04:03.1333333+00:00

    The return value of a .Where() needs to be a bool, but x.ListPosition.Where(y => y.PanelEdge == 0) gives you an IEnumerable<>. I think maybe you might be after a x.ListPosition.Any(y => y.PanelEdge == 0) instead?

    For the second question, there's nothing wrong with multiple breaks, as the alternative would be something like a goto to jump to the top level, but it's highly discouraged using goto. If you go down the LINQ route then .Any() will bail out early (after the first match).


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more