1

I have a method that returns an IEnumerable of this type:

public class ProductUpdate
{
    public string ProductId { get; set; }
    public DateTime DueDateTime { get; set; }
}

and I have a

List<string>

which has a list of dates as strings.

What I am trying to do is check for any Product that has a DueDate value which matches an item in the List of strings. Remove it if there is a match.

Ex:

Let's say a ProductUpdate item, PU1, in the IEnumerable has a DueDate 06/07/2015 and the List of strings contains 60/07/2015, then remove PU1 from the IEnumerable collection.

I could get it done using a foreach but am looking at a solution with LINQ.

Thanks in advance.

Codehelp
  • 4,157
  • 9
  • 59
  • 96

4 Answers4

3

Here you go (if productsUpdates is a list):

productsUpdates.RemoveAll(pu => listOfStrings.Any(s => s == pu.DueDateTime.ToString("MM/dd/yyyy"));

if productsUpdate is IEnumerable use this:

var result = productsUpdates.Where(pu => listOfStrings.Any(s => s != pu.DueDateTime.ToString("MM/dd/yyyy"));

Edited: As Tim Shmelter points out, you may get wrong result with another culture, so comparing DateTime objects is a better option than comparing strings (see his answer). However, comparing DateTime objects will also compare hours, minutes, seconds etc., that are not contained in strings provided by you. If they are always empty (by empty I mean minimal value), it is ok, otherwise you should use my option.

Alex Sikilinda
  • 2,928
  • 18
  • 34
  • That second approach does the opposite, it keeps all with a matching date, he wants to remove them. It also works only if your date-separator is `/`, otherwise `ToString("MM/dd/yyyy")` replaces `/` with the actual date separator of your current culture. – Tim Schmelter Jun 17 '15 at 09:51
3

So you want to remove all ProductUpdate instances from the sequence which have a match in the string-list according to the date? Since an IEnumerable<T> does not support Remove you have to re-create it:

productUpates = productUpates
    .Where(pu => !datesList  // your List<string>
        .Any(str => pu.DueDateTime == DateTime.Parse(str, CultureInfo.InvariantCulture)));

If you want a list you can use productUpates.ToList().

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

complete code

List<ProductUpdate> _products = new List<ProductUpdate>();
_products.Add(new ProductUpdate{ProductId = "Prod1", DueDateTime =  new DateTime(2015,06,12)});
_products.Add(new ProductUpdate{ProductId = "Prod2", DueDateTime =  new DateTime(2015,01,13)});
_products.Add(new ProductUpdate{ProductId = "Prod3", DueDateTime =  new DateTime(2015,09,13)});
_products.Add(new ProductUpdate{ProductId = "Prod4", DueDateTime =  new DateTime(2015,12,18)});
_products.Add(new ProductUpdate{ProductId = "Prod5", DueDateTime =  new DateTime(2015,02,28)});
_products.Add(new ProductUpdate{ProductId = "Prod6", DueDateTime =  new DateTime(2015,08,01)});

List<string> _dueDates =new List<string>();
_dueDates.Add("08/01/2015");

_products.RemoveAll(entry => _dueDates.Any(date => date == entry.DueDateTime.ToString("MM/dd/yyyy"))); 
manish
  • 1,450
  • 8
  • 13
0

Try this alternative way

IEnumerable<ProductUpdate> productUpdate ;//Suppose this is your  IEnumerable
List<string> strDates = new List<string> ();//Suppose this is your  List of date string
strDates.Add("06/07/2015");//Add some dates
strDates.Add("06/17/2015");
...
....
productUpdate.Where(a => !str.Contains(a.DueDateTime.ToString("MM/dd/yyyy")));//this gives result what you expecting.
sangram parmar
  • 8,462
  • 2
  • 23
  • 47