简体   繁体   中英

Is there a way for a getter to return specific dictionary entries<string, someClass> based on a list of strings?

In my WPF application i'm trying to retrieve only specific dictionary entries based on a list of keys that the user can edit.

I'm trying to go about this by having a List<string> ListOfKeys that the user can modify, a Dictionary<string, AnObject> AllObjects that contains a bunch of AnObjects with different variables, and a Dictionary<string, AnObject> SomeObjects with a get{ } containing a query to only return the entries from AllObjects where the key in AllObjects matches an entry in the ListOfKey s.

What do i put in the query to make this work? Additionally, any suggestions for another way i should go about this?

XAML:

<ItemsControl ItemsSource="{Binding SomeObjects.Values}" Grid.Row="1">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding someText}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

C#

public class AClass
{
    string someText;
    int someNumber
    [...] etc.
}

public Dictionary<string, AClass> AllObjects;

public List<string> ListOfKeys;

public Dictionary<string, AClass> SomeObjects
{
    get
    {
        return AllObjects.Where(a key in AllObjects matches an entry in the ListOfKeys);
    }
}

Returning to a list instead of a dictionary:

List<AClass> someObjects
get
{
    return AllObjects.Where(x => ListOfKeys.Contains(x.Key)).Select(x => x.Value).toList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM