2011-01-05 107 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; 
    } 
}