Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从类列表中包含的列表中删除字符串_C#_Loops_Reference - Fatal编程技术网

C# 从类列表中包含的列表中删除字符串

C# 从类列表中包含的列表中删除字符串,c#,loops,reference,C#,Loops,Reference,我在下面的列表中有一个问题: List<MyClass> problemList = new List<MyClass>; 有人知道怎么做吗? 提前谢谢 我不确定您想要哪个版本,因此我将留下两个版本: 这将从每个问题列表的ForumList中删除与FForum中的项目匹配的问题列表 foreach (var problemItem in problemList) { problemItem.ForumList = problemItem.ForumList

我在下面的列表中有一个问题:

List<MyClass> problemList = new List<MyClass>;
有人知道怎么做吗?
提前谢谢

我不确定您想要哪个版本,因此我将留下两个版本:

这将从每个
问题列表
的ForumList中删除与FForum中的项目匹配的问题列表

foreach (var problemItem in problemList)
{
    problemItem.ForumList = problemItem.ForumList
        .Except(fForums);
}

在此版本中,您需要查看每个
问题列表
项。对于该项,请查看是否有任何
ForumnList
元素与
fforum
中的项匹配。如果存在任何匹配项,请排除
问题列表
元素

var filteredList = problemList
    .Where(x => !x.ForumList.Intersect(fForums).Any())
    .ToList();

Intersect
将返回
x.ForumList
fforum
中的元素。因此,如果存在任何匹配的,则应将其排除在外。

最简单的方法是在每次迭代中为forumList创建一个新副本。像这样:

foreach (var i in problemList)
    List fList = new ArrayList(i.ForumList);
    foreach (var n in fList)
        if (!fForums.Contains(n))
                     i.ForumModList.Remove(n);

这样,您就不会编辑您正在迭代的列表,而是它的一个副本。

如果我知道您想从problemList的ForumList中删除所有未找到的条目。您可以执行以下操作:

problemList.ForumList.RemoveAll(f => !fForums.Contains(f));
通过以下方式进行测试:

MyClass problemList = new MyClass();
problemList.ForumList.Add("A");
problemList.ForumList.Add("B");
problemList.ForumList.Add("C");
problemList.ForumList.Add("D");
problemList.ForumList.Add("E");

List<string> fForums = new List<string>();
fForums.Add("C");
fForums.Add("D");

problemList.ForumList.RemoveAll(f => !fForums.Contains(f));
MyClass problemList=new MyClass();
problemList.ForumList.Add(“A”);
问题列表。论坛列表。添加(“B”);
problemList.ForumList.Add(“C”);
problemList.ForumList.Add(“D”);
problemList.ForumList.Add(“E”);
List fForums=新列表();
f.添加(“C”);
f.添加(“D”);
problemList.ForumList.RemoveAll(f=>!fForums.Contains(f));
problemList.ForumList
剩下两项,
C
D


这就是您要找的吗?

最明显的方法是创建两个论坛列表的交叉点,并将其分配给属性:

problemList.ForEach(mc => mc.ForumList = mc.ForumList.Intersect(fForums).ToList());
对于以下示例:

List<MyClass> problemList = new List<MyClass>
{
    new MyClass {ForumList = new List<string>{"aaa", "bbb", "ccc"}},
    new MyClass {ForumList = new List<string>{"aaa", "bbb"}},
    new MyClass {ForumList = new List<string>{"xxx", "yyy"}},
};

List<string> fForums = new List<string> {"aaa", "bbb"};

problemList.ForEach(mc => mc.ForumList = mc.ForumList.Intersect(fForums).ToList());
List problemList=新列表
{
新MyClass{ForumList=新列表{“aaa”、“bbb”、“ccc”},
新MyClass{ForumList=新列表{“aaa”,“bbb”},
新MyClass{ForumList=新列表{“xxx”,“yyy”},
};
List fForums=新列表{“aaa”、“bbb”};
problemList.ForEach(mc=>mc.ForumList=mc.ForumList.Intersect(fForums.ToList());
problemList中的项的ForumList值如下:

1:
“aaa”、“bbb”

2:
“aaa”、“bbb”


3:

我认为将我的外部循环更改为for可能有效。我必须在周一早上试试,谢谢!这是复制品还是参考品?。星期一早上我回来的时候我得试试。感谢您的帮助,它将创建一个副本。太好了!。星期一早上我回去工作的时候我得试试。我喜欢你的解决方案,因为它很紧凑。试图使用.where.select.remove.contains,但无法找出正确的组合。@chris我不确定我是否正确解释了问题:)希望它对您有用。它不起作用,因为我有一个列表。您的解决方案只适用于单个类对象。@chris您只需
problemList.ForEach
然后执行我所做的操作即可。。。这只是另一层,所以应该可以正常工作。我认为我的解决方案更有效,因为它编辑当前列表,而不是创建新版本并重新分配。
MyClass problemList = new MyClass();
problemList.ForumList.Add("A");
problemList.ForumList.Add("B");
problemList.ForumList.Add("C");
problemList.ForumList.Add("D");
problemList.ForumList.Add("E");

List<string> fForums = new List<string>();
fForums.Add("C");
fForums.Add("D");

problemList.ForumList.RemoveAll(f => !fForums.Contains(f));
problemList.ForEach(mc => mc.ForumList = mc.ForumList.Intersect(fForums).ToList());
List<MyClass> problemList = new List<MyClass>
{
    new MyClass {ForumList = new List<string>{"aaa", "bbb", "ccc"}},
    new MyClass {ForumList = new List<string>{"aaa", "bbb"}},
    new MyClass {ForumList = new List<string>{"xxx", "yyy"}},
};

List<string> fForums = new List<string> {"aaa", "bbb"};

problemList.ForEach(mc => mc.ForumList = mc.ForumList.Intersect(fForums).ToList());