我有一个int类型的词典,myclass。 在myclass中,我有以下属性:字符串,int和字符串数组。
当我将数据保存在字典中时,Evrything工作正常。 但我现在想要查看字典,如果在myclass中有一个值,位于字符串数组。并过滤它们。例如,过滤掉Array" German"中包含的每个对象。 是否有可能这样做?
MyClass的:
public class MYCLASS
{
public int ID
{
get;
set;
}
public string Specialty
{
get;
set;
}
public string[] Language
{
get;
set;
}
public myclass(int id, string specialty, string[] language)
{
this.Language= language;
this.ID= id;
this.Specialty = specialty;
}
}
我的词典
Dictionary<int, myclass> objdict= new Dictionary<int, myclass>();
答案 0 :(得分:2)
是的,搜索字典的值,然后过滤掉数组
var dict = new Dictionary<int, MYCLASS>();
var filteredPairs = from pair in dict
where !pair.Value.Language.Contains("German")
select pair;
答案 1 :(得分:1)
如果我理解你的问题,如果你有一个班级:
public class Country
{
public string CountryName { get; set; }
public int CountryId { get; set; }
public string[] States { get; set; }
}
如果您有字典:
Dictionary<int, Country> dict = new Dictionary<int, Country>()
{
{1,new Country(){ CountryId=1,CountryName="Test1", States=new [] {"State1","State2","State3" }}},
{2,new Country(){ CountryId=1,CountryName="Test2", States=new []{"State11","State21","State31" }}},
{3,new Country(){ CountryId=1,CountryName="Test3", States=new []{"State12","State2","State31" }}},
{4,new Country(){ CountryId=1,CountryName="Test4", States=new []{"Stae112","State21","State31" }}},
};
然后您可以使用Where
IEnumerable<KeyValuePair<int,Country>> enumerable= dict.Where(x=>x.Value.States.Contains("State21",StringComparer.OrdinalIgnoreCase));
答案 2 :(得分:1)
使用LINQ是最好的选择。如果你想在常规循环中访问你的类,它将是这样的:
foreach (KeyValuePair<string, MYCLASS> entry in MyDic)
{
// Value is in: entry.Value and key in: entry.Key
foreach(string language in ((MYCLASS)entry.Value).Language)
{
//Do sth with next language...
}
}
答案 3 :(得分:0)
您可以使用LINQ表达式查找与您的查询匹配的记录:
var dictionary = new Dictionary<int, MYCLASS>();
var results = dictionary.Where(record => !record.Value.Language.Any("German"));