Magnus Jansson
Magnus Jansson

Reputation: 23

Adding LINQ select queries to expression in loop

I have a menu that dynamically populates a list of ToggleMenuFlyoutItems.

Now I want to filter a list based on the checked items in the ToggleMenuFlyoutItem list.

foreach(var treslag in TreeTypeFlyout.Items)
{
    var flyoutItem = treslag as ToggleMenuFlyoutItem;

    if (flyoutItem.IsChecked)
    {
        tempStems = tempStems.Where(x => 
                        x.SpeciesGroupName.ToLower() == flyoutItem.Text.ToLower());
    }
}

What I expect from this code is that my linq query expression is populated with a bunch of where clauses, but it's always one expression. It does not seem to add an expression for each checked item.

What am I missing?

Upvotes: 0

Views: 190

Answers (1)

D-Shih
D-Shih

Reputation: 46239

You can try to create a List to fill tempStems filter objects.

List<Type_of_tempStems> list =new List<Type_of_tempStems>();

foreach(var treslag in TreeTypeFlyout.Items)
{
    var flyoutItem = treslag as ToggleMenuFlyoutItem;

    if (flyoutItem.IsChecked)
    {
        list.AddRange(tempStems.Where(x => x.SpeciesGroupName.ToLower() == flyoutItem.Text.ToLower()));
    }
}

If I understand correctly you can try to use lambda Where and Join to make it.

TreeTypeFlyout.Items
    .Where(x=>x.IsChecked)
    .Join(x=> x.Text.ToLower(),y=>y.SpeciesGroupName.ToLower(),(x,y)=>y);

Upvotes: 1

Related Questions