2011-01-05 106 views
1

这很复杂,可能很简单。c#Linq从字典中的列表中过滤掉记录

1)我有一本字典。 (Variable_output)

2)在NotificationWrapper中我有一个列表。

3)在这个列表里面我有一些需要匹配的需求。

4)如果这些要求匹配,我想从字典中返回NotificationWrapper。 (_output.value)

我想是这样的:

var itemsToSend = 
    _output.Where(
     z => z.Value.Details.Where(
      x => DateTime.Now >= x.SendTime && 
      x.Status == SendStatus.NotSent && 
      x.TypeOfNotification == UserPreferences.NotificationSettings.NotificationType.Email 
    ) 
).Select().ToList(); 

所以我想匹配某条目自身内部条件的_output条目。因此,对于我循环的每个条目,我检查该条目中列表中的值,以查看它是否已发送。如果它没有被发送,那么我想返回_output.value项。 itemsToSend应该包含尚未发送的_output条目。 (不是_output.value.xxx里面的一些值)

回答

5

谷歌浏览器:)

var itemsToSend = _output 
    .Values 
    .Where(n => n.Details.Any(
     x => DateTime.Now >= x.SendTime && 
     x.Status == SendStatus.NotSent && 
     x.TypeOfNotification == UserPreferences.NotificationSettings.NotificationType.Email)) 
    .ToList(); 

即编译我想你正在寻找Any()

+0

+1我认为你是对的。 – 2011-01-05 10:42:25

+0

工作就像一个魅力。 – Patrick 2011-01-05 10:45:17

+0

“在谷歌浏览器中编译”? – Dykam 2011-01-05 11:00:36

0

是这样的吗?

public partial class Form1 : Form 
{ 
    public Number SomeNumber { get; set; } 
    public Form1() 
    { 
     InitializeComponent(); 

     var _output = new Dictionary<int, List<Number>> 
          { 
           { 
            1, new List<Number> 
             { 
              new Number {details = new Number.Details{a = true, b = true, c = true}}, 
              new Number {details = new Number.Details{a = false, b = false, c = false}}, 
              new Number {details = new Number.Details{a = true, b = true, c = false}}, 
              new Number {details = new Number.Details{a = false, b = false, c = false}}, 
              new Number {details = new Number.Details{a = true, b = true, c = true}}, 
             } 
            } 
          }; 

     var itemsToSend = (from kvp in _output 
          from num in kvp.Value 
          where num.details.a && num.details.b && num.details.c 
          select num).ToList(); 
    } 

} 

public class Number 
{ 
    public Details details { get; set; } 
    public class Details 
    { 
     public bool a; 
     public bool b; 
     public bool c; 
    } 
}