简体   繁体   English

c#-在if语句中使用lambda表达式的输入参数

[英]c# - Using input parameter of lambda expression in if statement

I have if statement like this: 我有这样的if语句:

if (myList.Any(x => s.Contains(x))
{
//some code here
}

in which I check if there is a string in myList which is contained in a string s. 在其中检查myList中是否有包含在字符串s中的字符串。 Is it somehow possible to get the element x of this list and use it in the if statement showed above (in "//some code here" part), when the condition is met? 当满足条件时,是否可以某种方式获取此列表的元素x并在上面显示的if语句(在“ //一些代码在这里”部分中)中使用它?

Thank you. 谢谢。

Switch from Any to FirstOrDefault , that will return the item that matched the test or null if no item was matched. Any切换为FirstOrDefault ,它将返回与测试匹配的项目;如果没有匹配的项目,则返回null。

var found = myList.FirstOrDefault(x => s.Contains(x));
if (found != null)
{
//some code here
}

If null could be considered a "valid value" for a element in myList you can create a extension method TryFirst 如果可以将null视为myList元素的“有效值”,则可以创建扩展方法TryFirst

public static class ExtensionMethods
{
    public static bool TryFirst<T>(this IEnumerable<T> @this, Func<T, bool> predicate, out T result)
    {
        foreach (var item in @this)
        {
            if (predicate(item))
            {
                result = item;
                return true;
            }
        }
        result = default(T);
        return false;
    }
}

This would let you do 这会让你做

string foundString;
var wasFound = myList.TryFirst(x => s.Contains(x), out foundString);
if (wasFound)
{
//some code here
}

and tell the difference between a null in your list and a default result. 并告诉您列表中的null和默认结果之间的区别。

The above two methods only act on the first item on the list that the Contains will match, if you want to act on all the items use a Where( clause and a foreach 如果要对所有项目执行操作,以上两种方法仅对Contains将匹配的列表中的第一个项目起作用,请使用Where(子句和foreach

foreach(var item in myList.Where(x => s.Contains(x))
{
//some code here
}

You must promise you will not use the following code and use one of the other options first 您必须保证不会使用以下代码,并首先使用其他选项之一

You can also do your stated question, it is possible to get a variable assigned to inside lambada. 您也可以做您陈述的问题,有可能将变量分配给lambada内部。 However this can not be done with expression lambadas, only with statement lambadas. 但是,这不能通过表达式lambadas来完成,而只能通过语句lambadas来完成。

string matachedString = null;
if (myList.Any(x => { var found = s.Contains(x);
                      if(found)
                          matachedString = x;
                      return found;
                     });
{
//some code here
}

But only do this option as a last resort, use one of the more appropriate methods like FirstOrDefaut or write a custom method like TryFirst first. 但仅仅做到这一点的选择作为最后的手段,使用像更合适的方法之一FirstOrDefaut或写这样一个自定义的方法TryFirst第一。

I'd use foreach / Where() for this, even if I'm only expecting 0 or 1 result: 我会为此使用foreach / Where() ,即使我只期望0或1个结果:

foreach (var item in myList.Where(x => s.Contains(x)))
{
    //some code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM