简体   繁体   English

有没有办法让 getter 返回特定的字典条目<string, someClass>基于字符串列表?

[英]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.在我的 WPF 应用程序中,我试图根据用户可以编辑的键列表仅检索特定的字典条目。

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.我试图通过具有去这个List<string> ListOfKeys用户可以修改,一个Dictionary<string, AnObject> AllObjects包含了一堆不同的变量AnObjects的,和Dictionary<string, AnObject> SomeObjects与一get{ }包含查询只从返回的条目AllObjects其中的关键AllObjects匹配的条目ListOfKey秒。

What do i put in the query to make this work?我在查询中输入什么来完成这项工作? Additionally, any suggestions for another way i should go about this?此外,对我应该采取的另一种方式有什么建议吗?

XAML: XAML:

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

C# 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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