Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 在泛型列表的ForEach()中的lambda表达式中使用条件运算符?_C#_.net 3.5_Lambda_Conditional Operator - Fatal编程技术网

C# 在泛型列表的ForEach()中的lambda表达式中使用条件运算符?

C# 在泛型列表的ForEach()中的lambda表达式中使用条件运算符?,c#,.net-3.5,lambda,conditional-operator,C#,.net 3.5,Lambda,Conditional Operator,在ForEach中的lambda表达式中不允许有条件运算符吗 List<string> items = new List<string>{"Item 1", "Item 2", "Item I Care About"}; string whatICareAbout = ""; // doesn't compile :( items.ForEach(item => item.Contains("I Care About") ? whatICareAbout +=

在ForEach中的lambda表达式中不允许有条件运算符吗

List<string> items = new List<string>{"Item 1", "Item 2", "Item I Care About"};

string whatICareAbout = "";

// doesn't compile :(
items.ForEach(item => item.Contains("I Care About") ? 
whatICareAbout += item + "," : whatICareAbout += "");

只是不可能?

您使用的是较短形式的lambda表达式,它只允许一个表达式。
您需要修改长表单,它允许多个语句

例如:

items.ForEach(item => {
    if (item.Contains("I Care About")) 
        whatICareAbout += item + ", ";
});
试试括号:

items.ForEach(item => item.Contains("I Care About") ? (whatICareAbout += item + ",") : (whatICareAbout += "") );
+=的优先级高于?,这可能就是您得到错误的原因。使用括号,错误可能会消失。但不是100%确定。。。lambda表达式可能有其他限制,从而阻止使用赋值语句

更新:

与其使用多个+=语句,不如将条件放在赋值的右侧,如下所示:

List<string> items = new List<string> { "one", "two", "three" };
string whatICareAbout = "";
items.ForEach(item => whatICareAbout +=  item.Contains("I Care About") ? (item + ",") : ""); 

但我认为上面的答案(我刚在发布本文时看到的)甚至比我的示例更好,因为它涉及删除终端“,”。看看他的回答…:-)

你想做什么?您是否试图在包含特定值的位置形成逗号分隔的项目字符串?在linq中,您可以通过以下方式实现这一点:

 List<string> items = new List<string> { "Item 1", "Item 2", "Item I Care About", "Item I Care About", "Item I Care About" }; 
 string whatICareAbout = items.Where(x => x.Contains("I Care About"))
                              .Aggregate( (y, z) => y + ", " + z);
List items=新列表{“项目1”、“项目2”、“我关心的项目”、“我关心的项目”、“我关心的项目”};
string whaticarearout=items.Where(x=>x.Contains(“我关心”))
.骨料((y,z)=>y+“,”+z);
这个输出是“我关心的项目,我关心的项目,我关心的项目”


注意:聚合是确保没有尾随“,”的一种很好的方法。

问题在于该表达式

item.Contains("I Care About") ? whatICareAbout += item + "," : whatICareAbout += ""
不是一个语句。它只返回一个类型为
string
的值

有一个技巧可以让它工作(只是为了好玩):


我只是简单地添加了对
.GetType()
方法的调用,以从初始表达式创建一个语句,并对其进行了编译。

对sting concat使用
聚合是一个非常巧妙的想法,我没有想到。您可以轻松地将其更改为使用
StringBuilder
,因为
StringBuilder
具有流畅的界面。哈,我刚刚在初始答案中添加了一个聚合()代码示例。。。但我更喜欢你的+1.Sheesh这些答案给我留下了非常深刻的印象,都是在提问的几分钟内:)我认为@SLaks回答了我问题的意图,但Aggregate()让我大吃一惊:)再次感谢大家:)您有一个字符串表达式作为条件运算符的类型,并且您需要一个用于.ForEach()方法的语句。这就是为什么会有编译错误。@Roman-我现在明白了!我想到了一个?x:y是if(a){x}else{y}but的缩写?返回x和y的值,在本例中是字符串…对于
.ForEach()
您需要一个语句:)
 List<string> items = new List<string> { "Item 1", "Item 2", "Item I Care About", "Item I Care About", "Item I Care About" }; 
 string whatICareAbout = items.Where(x => x.Contains("I Care About"))
                              .Aggregate( (y, z) => y + ", " + z);
item.Contains("I Care About") ? whatICareAbout += item + "," : whatICareAbout += ""
    items.ForEach(item => (item.Contains("I Care About") ?
    whatICareAbout += item + "," : whatICareAbout += "").GetType());