简体   繁体   中英

c# Linq filter out records from a list within a dictionary

This is complicated to explain and probably very simple to do.

1) I have a dictionary . (Variable _output)

2) Inside the NotificationWrapper i have a list.

3) Inside this list i have some requirements that i need to be matched.

4) If those requirements are matched i want to return the NotificationWrapper from the dictionary. (_output.value)

I tried something like this:

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();

So i want the _output entries that matches the condition inside the entry itself. So for each entry i loop through, i check the values inside the list in that entry to see if its been sent or not. If it hasn't been sent, then i want to return that _output.value item. itemsToSend should contain _output entries that hasn't been sent. (Not some values inside _output.value.xxx)

Compiled in Google Chrome :)

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();

Ie I think you're looking for Any() .

Something like this?

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM