var myDictionary = new Dictionary<int, List<DateTimeOffset?>>
{
{1234, new List<DateTimeOffset?> {new DateTimeOffset(DateTime.Today), null, new DateTimeOffset(DateTime.Today)}},
{2134, new List<DateTimeOffset?> {null, new DateTimeOffset(DateTime.Today), new DateTimeOffset(DateTime.Today)}},
{2345, new List<DateTimeOffset?> {null, null, new DateTimeOffset(DateTime.Today)}},
{3451, new List<DateTimeOffset?> {new DateTimeOffset(DateTime.Today).AddDays(-1), null, new DateTimeOffset(DateTime.Today)}}
};
当我通过DateTimeOffset(DateTime.Today)搜索时,我只想选择或提取键1234,2134和2345而不是最后一个3451,因为并非此键的所有值都等于搜索的datetimeoffset值DateTimeOffset( DateTime.Today)忽略空值。
我需要一个linq查询。这个查询好像是这样做但是如何提取字典的键???
var mySearchResult = myDictionary.Values.Where(v1 => v1.All(d2 => d2.HasValue == false || d2.Value == new DateTimeOffset(DateTime.Today)));
非常感谢任何帮助。
答案 0 :(得分:1)
这对我有用:
var keys =
myDictionary
.Where(kvp => kvp.Value.All(d =>
d.HasValue == false || d.Value == new DateTimeOffset(DateTime.Today)))
.Select(kvp => kvp.Key);
我明白了:
1234
2134
2345