简体   繁体   English

使用LINQ从IEnumerable中删除与列表中的项目匹配的项目

[英]Removing items from an IEnumerable which match items in a List using LINQ

I have a method that returns an IEnumerable of this type: 我有一个返回此类型IEnumerable的方法:

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. 我想做的是检查是否有DueDate值与字符串列表中的某个项目匹配的任何Product。 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. 假设IEnumerable中的ProductUpdate项PU1具有DueDate 06/07/2015,字符串列表包含60/07/2015,然后从IEnumerable集合中删除PU1。

I could get it done using a foreach but am looking at a solution with LINQ. 我可以使用foreach来完成它,但是我正在寻找LINQ的解决方案。

Thanks in advance. 提前致谢。

Here you go (if productsUpdates is a list): 您可以在这里查看(如果productsUpdates是列表):

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

if productsUpdate is IEnumerable use this: 如果productsUpdateIEnumerable使用以下命令:

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). 编辑 :正如Tim Shmelter指出的那样,使用其他区域性可能会导致错误的结果,因此比较DateTime对象比比较字符串更好(参见他的回答)。 However, comparing DateTime objects will also compare hours, minutes, seconds etc., that are not contained in strings provided by you. 但是,比较DateTime对象还将比较您提供的字符串中未包含的小时,分​​钟,秒等。 If they are always empty (by empty I mean minimal value), it is ok, otherwise you should use my option. 如果它们始终为空(用空表示我的意思是最小值),则可以,否则,应使用我的选项。

So you want to remove all ProductUpdate instances from the sequence which have a match in the string-list according to the date? 因此,您要根据日期从字符串列表中匹配的序列中删除所有ProductUpdate实例吗? Since an IEnumerable<T> does not support Remove you have to re-create it: 由于IEnumerable<T>不支持Remove ,因此必须重新创建它:

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() . 如果需要列表,可以使用productUpates.ToList()

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

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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