简体   繁体   English

从对象列表中删除特定项目

[英]Remove specific items from list of objects

I have list of objects: 我有对象列表:

public class SDesc
{
    public string sensorId { get; set; }
    public string address { get; set; }
}

List<SDesc> desc = new List<SDesc>
{
    new SDesc {sensorId = "1234", address =   "Adams22"},
    new SDesc {sensorId = "5555", address =   "Hourton34"},
    new SDesc {sensorId = "4444", address =   "SaintsRoad55"},
    new SDesc {sensorId = "1258", address =   "BerryAve58"},
    new SDesc {sensorId = "52486", address =   "SaintsRoad2"},
    new SDesc {sensorId = "12361", address =   "TomassonRoad"}
}

And also I have IEnumarable of strings: 而且我还有IEnumarable的字符串:

IEnumarable<string> sId = {"4444","52486","12361"};

from desc list I need to remove records where sensorsId property exists in sId list. desc列表中,我需要删除sId列表中sensorId属性存在的记录。

For example for case above the result I want to get is: 例如,对于上述情况,我想要得到的结果是:

List<SDesc> desc = new List<SDesc>
{
    new SDesc {sensorId = "1234", address =   "Adams22"},
    new SDesc {sensorId = "5555", address =   "Hourton34"},
    new SDesc {sensorId = "1258", address =   "BerryAve58"},
}

Here what I tried: 这是我尝试的:

desc.RemoveAll(obj => obj.sensorId == sId);

But it's not works properly because sID is IEnumarable type. 但这不能正常工作,因为sID是IEnumarable类型。

So my question is how to remove items from desc list where sensorsId property exists in sId list? 所以我的问题是如何从sId列表中存在sensorId属性的desc列表中删除项目?

You need to use Any with it like: 您需要使用Any ,例如:

 desc.RemoveAll(obj => sId.Any(x=> x== obj.sensorId ));

As method name suggests it would check if any of the item in sId matches with the item in desc against sensorId , it will remove those items from List<T> . 正如方法名称所暗示的那样,它将针对sensorId检查sId中的任何项目与desc中的项目sId匹配,它将从List<T>删除这些项目。

You can use .Contains() LINQ method to check if a collection contains an item: 您可以使用.Contains() LINQ方法来检查集合中是否包含项:

desc.RemoveAll(obj => sId.Contains(obj.sensorId));

However, it would lead to multiple enumeration of enumerable sId . 但是,这将导致可枚举sId多个枚举。 It's not a problem in this case since this enumerable is an array in this particular case . 在这种情况下这不是问题,因为在这种特殊情况下,此可枚举是一个数组。

Read more about "possible multiple enumeration": 阅读有关“可能的多重枚举”的更多信息:
- Handling warning for possible multiple enumeration of IEnumerable - 处理可能的IEnumerable多个枚举的警告
- Resharper's example code for explaining "Possible multiple enumeration of IEnumerable" -Resharper的示例代码,用于解释“ IEnumerable的可能多重枚举”

I would recommend converting it to a collection to make sure you enumerate IEnumerable only once. 我建议将其转换为集合,以确保只枚举IEnumerable
As suggested by Evk in comments, it is better to use HashSet so that .Contains executes in O(1) time: 正如Evk在评论中建议的那样,最好使用HashSet以便.ContainsO(1)时间执行:

List<SDesc> desc = new List<SDesc> {
  new SDesc {sensorId = "1234", address =   "Adams22"},
  new SDesc {sensorId = "5555", address =   "Hourton34"},
  new SDesc {sensorId = "4444", address =   "SaintsRoad55"},
  new SDesc {sensorId = "1258", address =   "BerryAve58"},
  new SDesc {sensorId = "52486", address =   "SaintsRoad2"},
  new SDesc {sensorId = "12361", address =   "TomassonRoad"}
};

IEnumarable<string> sId = {"4444","52486","12361"};
var sIdsSet = new HashSet(sId);

desc.RemoveAll(obj => sIdsSet.Contains(obj.sensorId));

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

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