简体   繁体   中英

LINQ - selecting elements with a specific day

I have a class which has one of the properties as DateTime.

public class ClassWithDate
{
   public int somevalue {get;set;}
   public DateTime somedate {get;set;}
}

I have another List of string which holds values of day (Monday, Tuesday, etc.)

List<string> someDays;

I have an IEnumerable list of the ClassWithDate

IEnumerable<ClassWithDate> classWithDates;

I wish to select those elements from classWithDates where the date property is NOT a day from the someDays list of string

Something like this:

classWithDates.someDate.DayOfWeek != someDays

I know the syntax is wrong, but just wanted to provide an example.

You could do this.

var results = classWithDates.Where(wd=>!someDays.Contains(wd.somedate.DayOfWeek.ToString()));

or

var results = classWithDates.Where(wd=>!someDays.Any(s=> sq.Equals(wd.somedate.DayOfWeek.ToString(), StringComparison.OrdinalIgnoreCase)));

Working Example

Let List<string> days = new List<string>() { "Monday", "Tuesday", "Friday" }; be the defined list of week days; and classWithDates be your collection; then the following query will help you to do the task:

var selectedDates = classWithDates.Where(x => !days.Contains(x.somedate.DayOfWeek.ToString()));

An example here :

 DateTime d = DateTime.Now;
 List<ClassWithDate> classWithDates = new List<ClassWithDate>();
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(1), somevalue = 12 });
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(2), somevalue = 12 });
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(3), somevalue = 12 });
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(4), somevalue = 12 });
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(5), somevalue = 12 });
 classWithDates.Add(new ClassWithDate() { somedate = d.AddDays(6), somevalue = 12 });
 List<string> days = new List<string>() { "Monday", "Tuesday", "Friday" };
 var selectedDates = classWithDates.Where(x => !days.Contains(x.somedate.DayOfWeek.ToString()));

Try this

 List<ClassWithDate> _sampleList=new List<ClassWithDate>(); 
_sampleList.Add(new ClassWithDate(){somevalue=10,somedate=new DateTime(1980, 5, 5)});
_sampleList.Add(new ClassWithDate(){somevalue=10,somedate=new DateTime(1982, 10, 20)});
_sampleList.Add(new ClassWithDate(){somevalue=10,somedate=new DateTime(1984, 1, 4)});
_sampleList.Add(new ClassWithDate(){somevalue=10,somedate=new DateTime(1979, 6, 19)});



List<string> daysList = new List<string>() { "Monday", "Tuesday" };
IEnumerable<ClassWithDate> selectedDates = _sampleList.Where(x => !daysList.Contains(x.somedate.ToString("dddd")));

foreach(ClassWithDate cl in selectedDates)
{
  Console.WriteLine(cl.somedate.ToString("dddd"));      
}

Something like this:

  // Let be comparison case insensitive
  HashSet<String> hs = new HashSet(someDays, StringComparer.OrdinalIgnoreCase);

  // providing that classWithDates doesn´t contain null items
  var result = classWithDates
    .Where(item => !hs.Contains(item.somedate.DayOfWeek.ToString()));

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