Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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

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# 循环浏览列表和;return";除非没有匹配项_C#_Loops - Fatal编程技术网

C# 循环浏览列表和;return";除非没有匹配项

C# 循环浏览列表和;return";除非没有匹配项,c#,loops,C#,Loops,我试图循环遍历字符串列表,并对照单个字符串检查它们。如果没有找到匹配的代码,那么我们需要退出代码 // loadedObj.Settings contains the list of strings, can be any number of strings foreach (var currentCheckBox in loadedObj.Settings.Where(currentCheckBox => currentCheckBox != null)) {

我试图循环遍历字符串列表,并对照单个字符串检查它们。如果没有找到匹配的代码,那么我们需要退出代码

// loadedObj.Settings contains the list of strings, can be any number of strings     
foreach (var currentCheckBox in loadedObj.Settings.Where(currentCheckBox => currentCheckBox != null))
    {   
        // docTypeAlias is a single string that needs to be matched
        var docTypeAlias = sender.ContentType.Alias;
        // This is the current value of currentCheckBox
        var requiredTypeAlias = currentCheckBox;
        if (!requiredTypeAlias.Equals(docTypeAlias)) return;
    }

如果设置中只有一个字符串,则代码可以正常工作,但一旦有多个字符串,如果第一个字符串明显不匹配,则代码将很快退出。

您可以使用
Any
查看序列中的任何元素是否符合您的条件。如果没有人这样做,结果将是错误的

var docTypeAlias = sender.ContentType.Alias; 
bool hasMatch = loadedObj.Settings.Any(current => docTypeAlias.Equals(current));
if (hasMatch)
{
    // can work
}
else
{
    // can't work
}
看起来您希望“继续”(跳过foreach中的其余代码并检查下一项),而不是从函数返回的“return”


如果只需要检查集合上的某个谓词,请考虑使用FirstOrDefault。

添加布尔值以记录答案

bool found = false;
foreach (var currentCheckBox in loadedObj.Settings.Where(currentCheckBox => currentCheckBox != null)) 
{    
    // docTypeAlias is a single string that needs to be matched 
    var docTypeAlias = sender.ContentType.Alias; 
    // This is the current value of currentCheckBox 
    var requiredTypeAlias = currentCheckBox; 
    if (requiredTypeAlias.Equals(docTypeAlias)) {
        found = true;
        break;
    } 
}
if (!found) return;
或者,将其作为单独的功能:

bool ControlIsListed(object sender, MySettingsClass loadedObj)
{
    foreach (var currentCheckBox in loadedObj.Settings.Where(currentCheckBox => currentCheckBox != null)) 
    {    
        // docTypeAlias is a single string that needs to be matched 
        var docTypeAlias = sender.ContentType.Alias; 
        // This is the current value of currentCheckBox 
        var requiredTypeAlias = currentCheckBox; 
        if (requiredTypeAlias.Equals(docTypeAlias)) return true;
    }
    return false;
}
并称之为:

private void eventhandler(object sender, EventArgs e)
{
    if (!ControlIsListed(sender, loadedObj)) return;
    // ...
}

我想你可以用这个来代替,如果我理解的好的话

 loadedObj.Settings.Find(delegate(String currentCheckBox)
{
    return sender.ContentType.Alias == currentCheckBox
});

如果找到某个内容,它将返回
字符串
,如果不是
null

currentCheckBox
似乎是一个复选框。复选框怎么可能是字符串列表?代码也不会尝试任何与文本相关的内容。你能编辑以澄清类型和你的实际目标是什么吗?为什么不使用常规的׳foreach׳循环?对不起,这是从其他代码复制的名称,实际上是从xml文件读取的复选框项列表。然后你能编辑你的问题并使其更准确,以便将来对其他人有用吗?如果你的问题是无效的,而有人恰好猜到了你在寻找什么,并提供了一个你可以使用的答案,你仍然可以把你的问题做得更好,这样以后其他人可能会受益。谢谢。:)在我看来,这段代码就像是缩短了的示例-如果“发现”案例,Ian Houghton也应该做些什么。。。但对样品来说是的,因为它是没有用的。我认为示例是JQuery的翻译。返回的每个地方基本上都会继续。
 loadedObj.Settings.Find(delegate(String currentCheckBox)
{
    return sender.ContentType.Alias == currentCheckBox
});