LINQ从对象数组中删除项,其中属性等于值 [英] linq remove item from object array where property equals value

查看:1772
本文介绍了LINQ从对象数组中删除项,其中属性等于值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

IEnumberable<Car> list

和我想根据汽车的性能从这个名单中删除项目

and i want to remove an item from this list based on a property of the car

我想是这样的:

list.RemoveWhere(r=>r.Year > 2000)

做这样的事情存在。

我整整做这和过,所以我想避免每次复制列表中只删除一个项目

i am doing this over and over so i want to avoid copying the list each time to just remove one item

推荐答案

IEnumberable 是不变的,但你可以以这样的:

IEnumberable is immutable, but you can to something like this:

list = list.Where(r=>r.Year<=2000)

或写扩展方法:

public static IEnumerable<T> RemoveWhere<T>(this IEnumerable<T> query, Predicate<T> predicate)
{ 
    return query.Where(e => !predicate(e));
}

这篇关于LINQ从对象数组中删除项,其中属性等于值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆