从列表中删除项目

时间:2013-06-06 11:53:43

标签: c# asp.net-mvc

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

列表是:

IList<FeatureDto> featuresDto

featureDto是

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

我收到的身份证明格式为:

selectedFeatureIds = "4,5,6"

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

5 个答案:

答案 0 :(得分:3)

int[] toRemove = selectedFeatureIds.Split(',').Select(s => int.Parse(s)).ToArray();
featuresDto.RemoveAll(f => toRemove.Contains(f.Id));

答案 1 :(得分:1)

var ids = selectedFeatureIds.Split(',').Select(id => int.Parse(id)).ToList();
featuresDto.RemoveAll(feature => ids.Any(id => id == feature.Id));

答案 2 :(得分: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));

答案 3 :(得分:0)

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

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

之后循环遍历它们并从列表中删除

foreach(var id in inds)
{
   featuresDto.Remove().Where(x => x.Id == Int32.Parse(id));
}

答案 4 :(得分:0)

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

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

}