字典的形式與此類似:
var dictionary = new Dictionary<string, List<DummyObject<int, string, string>>>()
{
{ "one", new List<DummyObject<int, string, string>>()
{
new DummyObject<int, string, string>("ONE"),
new DummyObject<int, string, string>("TWO"),
new DummyObject<int, string, string>("THREE"),
new DummyObject<int, string, string>("FOUR")
}
},
{ "two", new List<DummyObject<int, string, string>>()
{
new DummyObject<int, string, string>("TWO"),
new DummyObject<int, string, string>("THREE"),
new DummyObject<int, string, string>("FOUR")
}
},
{ "three", new List<DummyObject<int, string, string>>()
{
new DummyObject<int, string, string>("THREE"),
new DummyObject<int, string, string>("FOUR")
}
}
};
虛擬物件:
public class DummyObject<TEntity, TActionType, TParams>
{
private TActionType _actionType;
public DummyObject(TActionType actionType)
{
_actionType = actionType;
}
public TActionType ActionType
{
get
{
return _actionType;
}
}
}
我正在嘗試撰寫一些東西來查找所有字典條目,其中它們的值有一個串列,其中包含 DummyObject“TWO”或“FOUR”或基本上任何可能存在于該欄位中的值(這當然是一個示例,但是圖片 30 種可能性)。
我只需要獲取字典的整個子集(在一個快速的 LinQ 陳述句中),然后我就可以根據鍵進行進一步的縮減(實際上這不僅僅是一個字串)。因此,對于“TWO”的示例,結果應該是一個如下所示的字典:
var subsetDictionary = new Dictionary<string, List<DummyObject<int, string, string>>>()
{
{ "one", new List<DummyObject<int, string, string>>()
{
new DummyObject<int, string, string>("ONE"),
new DummyObject<int, string, string>("TWO"),
new DummyObject<int, string, string>("THREE"),
new DummyObject<int, string, string>("FOUR")
}
},
{ "two", new List<DummyObject<int, string, string>>()
{
new DummyObject<int, string, string>("TWO"),
new DummyObject<int, string, string>("THREE"),
new DummyObject<int, string, string>("FOUR")
}
}
只是似乎無法找到合適的組合來獲得它。
uj5u.com熱心網友回復:
我認為您想要以下內容:
dictionary
.Where(entry => entry.Value
.Any(item => item.ActionType == "TWO"))
.ToDictionary(
entry => entry.Key,
entry => entry.Value);
uj5u.com熱心網友回復:
如果您需要實際條目,請嘗試:
dictionary.Where(x => x.Value.Any(x => x.ActionType == "ONE"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476605.html
