我有以下列表:列表中選擇範圍對象
List<MyType> myList = new List<MyType>
{
new MyType {Key = "aa", Value = "test1"},
new MyType {Key = "bb", Value = "test2"},
new MyType {Key = "zz", Value = "testzz"},
new MyType {Key = "cc", Value = "test3"},
new MyType {Key = "yy", Value = "testyy"},
new MyType {Key = "dd", Value = "test4"},
new MyType {Key = "ee", Value = "test5"}
};
其中,
public class MyType
{
public string Key { get; set; }
public string Value { get; set; }
}
現在,我想找回基於價值的範圍內的所有對象的關鍵。也就是說,我想選擇從鍵爲「BB」,以重點=「DD」(無字母排序)啓動列表中的所有對象,這樣我會有以下結果:
new MyType {Key = "bb", Value = "test2"},
new MyType {Key = "zz", Value = "testzz"},
new MyType {Key = "cc", Value = "test3"},
new MyType {Key = "yy", Value = "testyy"},
new MyType {Key = "dd", Value = "test4"}
如何我可以用linq/lambda表達式來實現嗎?
[更新:12/30/2015]:密鑰不按字母順序排列,可能有數百個密鑰。因此,涉及list.contains(..)和假定字母排序的解決方案將無法工作。我還更新了該示例,以包含鍵'yy'和'zz'的對象以反映相同的情況。
如何多樣可以將您的標準實際上是?他們總是在一個範圍內,還是可以完全脫節?取決於此,可能會有廣泛的不同答案。 – ChrisF