-5

我需要从匹配 id 的列表中删除项目:

名单是:

IList<FeatureDto> featuresDto

特征Dto是

public class FeatureDto
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

我收到的身份证的格式是:

selectedFeatureIds = "4,5,6"

我需要从IList<FeatureDto> featuresDto字符串 selectedFeatureIds 的 id 中删除。

4

5 回答 5

3
int[] toRemove = selectedFeatureIds.Split(',').Select(s => int.Parse(s)).ToArray();
featuresDto.RemoveAll(f => toRemove.Contains(f.Id));
于 2013-06-06T11:58:40.247 回答
1
var ids = selectedFeatureIds.Split(',').Select(id => int.Parse(id)).ToList();
featuresDto.RemoveAll(feature => ids.Any(id => id == feature.Id));
于 2013-06-06T11:59:33.610 回答
1

这是一种表达方式:

featuresDto
    .Select((i, index) => 
        selectedFeatureIds.Split(',').Contains(i.Id.ToString()) ? index : -1)
    .Where(i => i >= 0)
    .OrderByDescending(i=>i)
    .ToList()
    .ForEach(i => featuresDto.RemoveAt(i));
于 2013-06-06T12:05:45.430 回答
0

从 selectedFeatureIds 创建一个 int 数组(使用字符串拆分方法)

foreach(int i in int_Ary)
{
  foreach(FeatureDto f in featuresDto){
   if(f.id == i)
    featuresDto.Remove(f)
  }

}
于 2013-06-06T12:04:43.000 回答
0

首先将您的入口字符串拆分为数组。

var ids = selectedFeatureIds.Split(',');

在循环通过它们并从列表中删除之后

foreach(var id in inds)
{
   featuresDto.Remove().Where(x => x.Id == Int32.Parse(id));
}
于 2013-06-06T11:58:42.090 回答