【问题标题】:Removing items from an IEnumerable which match items in a List using LINQ使用 LINQ 从 IEnumerable 中删除与列表中的项目匹配的项目
【发布时间】:2015-06-17 09:42:08
【问题描述】:

我有一个返回这种类型的 IEnumerable 的方法:

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

我有一个

List<string>

它有一个日期列表作为字符串。

我要做的是检查任何具有与字符串列表中的项目匹配的 DueDate 值的产品。如果匹配,则将其删除。

例如:

假设 IEnumerable 中的 ProductUpdate 项 PU1 的 DueDate 日期为 06/07/2015,而字符串列表包含 60/07/2015,然后从 IEnumerable 集合中删除 PU1。

我可以使用 foreach 完成它,但我正在寻找使用 LINQ 的解决方案。

提前致谢。

【问题讨论】:

  • List 确实有一个 foreach 扩展方法。
  • 60/07/2015 是几号?
  • 您不能从 IEnumerable 中删除元素。您可以从您的 IEnumerable 创建一个新集合,而不需要不需要的元素。

标签: c# linq


【解决方案1】:

所以你想根据日期从序列中删除所有在字符串列表中匹配的ProductUpdate 实例?由于 IEnumerable&lt;T&gt; 不支持 Remove 您必须重新创建它:

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

如果你想要一个列表,你可以使用productUpates.ToList()

【讨论】:

    【解决方案2】:

    给你(如果productsUpdates 是一个列表):

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

    如果productsUpdateIEnumerable,请使用:

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

    已编辑: 正如 Tim Shmelter 指出的那样,您可能会在另一种文化中得到错误的结果,因此比较 DateTime 对象是比比较字符串更好的选择(请参阅他的答案)。 但是,比较 DateTime 对象还将比较小时、分钟、秒等,这些不包含在您提供的字符串中。如果它们总是空的(空的意思是最小值),没关系,否则你应该使用我的选项。

    【讨论】:

    • 第二种方法正好相反,它保留所有匹配的日期,他想删除它们。它也仅在您的日期分隔符为 / 时才有效,否则 ToString("MM/dd/yyyy")/ 替换为您当前文化的实际日期分隔符。
    【解决方案3】:

    完整代码

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

    【讨论】:

      【解决方案4】:

      试试这个替代方法

      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.
      

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多