列表< object> .RemoveAll - 如何创建适当的谓词 [英] List<object>.RemoveAll - How to create an appropriate Predicate

查看:156
本文介绍了列表< object> .RemoveAll - 如何创建适当的谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个没有问题的问题 - C#和泛型还是相当新鲜的,并且是全新的对于谓词,代表和lambda表达式...



有一个类查询,其中包含另一个类别车辆的通用列表。我正在建立从父查询添加/编辑/删除车辆的代码。目前,我正在专门看看删除。



从目前为止我已经看到,似乎我可以使用Vehicles.RemoveAll()删除具有特定VehicleID的项目或具有特定EnquiryID的所有项目。我的问题是了解如何进行.RemoveAll所有正确的谓词 - 我看到的例子太简单了(或者也许我太简单了,因为我缺乏对谓词,代表和lambda表达式的了解)。



所以如果我有一个列表< Of Vehicle>车辆其中每辆车有一个 EnquiryID ,我将如何使用 Vehicles.RemoveAll()删除给定EnquiryID的所有车辆?



我知道有几种方法,所以我很乐意听到方法之间的差异 - 我需要的要获得一些工作,这也是一个学习练习。



作为补充问题,这是一个泛型列表,这些对象最好的存储库?我的第一倾向是收藏,但似乎我已经过时了。当然,泛型似乎是首选,但我对其他替代方案很好奇。

解决方案

RemoveAll ()方法接受一个谓词< T> 委托(直到这里没有新的)。谓词指向简单返回true或false的方法。当然, RemoveAll 将从集合中删除所有使用谓词应用的返回True的 T 实例。 p>

C#3.0让开发人员使用几种方法将谓词传递给 RemoveAll 方法(而不仅仅是这一个...) 。您可以使用:



Lambda表达式

  vehicles.RemoveAll(车辆= vehicle.EnquiryID == 123); 

匿名方法

 code> vehicles.RemoveAll(delegate(Vehicle v){
return v.EnquiryID == 1;
});

正常方法

 code> vehicles.RemoveAll(VehicleCustomPredicate); 
private static bool
VehicleCustomPredicate(Vehicle v){
return v.EnquiryID == 1;
}


This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lambda expressions...

I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building up the code to add/edit/delete Vehicles from the parent Enquiry. And at the moment, I'm specifically looking at deletions.

From what I've read so far, it appears that I can use Vehicles.RemoveAll() to delete an item with a particular VehicleID or all items with a particular EnquiryID. My problem is understanding how to feed .RemoveAll the right predicate - the examples I have seen are too simplistic (or perhaps I am too simplistic given my lack of knowledge of predicates, delegates and lambda expressions).

So if I had a List<Of Vehicle> Vehicles where each Vehicle had an EnquiryID, how would I use Vehicles.RemoveAll() to remove all vehicles for a given EnquiryID?

I understand there are several approaches to this so I'd be keen to hear the differences between approaches - as much as I need to get something working, this is also a learning exercise.

As an supplementary question, is a Generic list the best repository for these objects? My first inclination was towards a Collection, but it appears I am out of date. Certainly Generics seem to be preferred, but I'm curious as to other alternatives.

解决方案

The RemoveAll() methods accept a Predicate<T> delegate (until here nothing new). A predicate points to a method that simply returns true or false. Of course, the RemoveAll will remove from the collection all the T instances that return True with the predicate applied.

C# 3.0 lets the developer use several methods to pass a predicate to the RemoveAll method (and not only this one…). You can use:

Lambda expressions

vehicles.RemoveAll(vehicle => vehicle.EnquiryID == 123);

Anonymous methods

vehicles.RemoveAll(delegate(Vehicle v) {
  return v.EnquiryID == 1;
});

Normal methods

vehicles.RemoveAll(VehicleCustomPredicate);
private static bool
VehicleCustomPredicate (Vehicle v) {
    return v.EnquiryID == 1; 
}

这篇关于列表&lt; object&gt; .RemoveAll - 如何创建适当的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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