C#.NET 4.0:可为空的输入字符串参数和Lambda表达式 [英] c# .net 4.0 : nullable input string parameter and the lambda expressions
问题描述
我有以下方法:
public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc)
{
return db.Profiles.Where(p => p.CountryFrom.CountryName.Equals(CountryFrom,StringComparison.OrdinalIgnoreCase));
}
现在,它仅按CountryFrom进行过滤,但我也需要按CountryLoc进行过滤. 那么,如何修改Where过滤器?
Right now, it just filters by CountryFrom but I need to filter it by CountryLoc as well. So how do I modify the Where filter?
此外,CountryFrom可以为null或CountryLoc可以为null.因此,如何将方法的签名修改为所有可为空的输入字符串参数.
Also, CountryFrom could be null or CountryLoc could be null. So how do I modify the method's signature to all the nullable input string parameter.
我知道如何在SQL中执行此操作,但是我不确定lambda表达式或LINQ.
I know how to do this in SQL but I am not sure about lambda expressions or LINQ.
谢谢
推荐答案
您的问题实际上并非特定于LINQ.您可以将任何布尔条件放入所需的lambda中,例如:
Your question isn't actually LINQ-specific. You can put any boolean conditions inside the lambda that you want, such as:
p => p.CountryFrom.CountryName.Equals(...) && p.CountryLoc.CountryName.Equals(...)
要处理空值,可以改用静态string.Equals
方法,如下所示:
To handle null values you can use the static string.Equals
method instead, like this:
p => string.Equals(p.CountryFrom.CountryName, CountryName, StringComparison.OrdinalIgnoreCase)
如果开始时间太长,您总是可以将其提取到方法中.
If this starts getting too long you could always extract it into a method.
这篇关于C#.NET 4.0:可为空的输入字符串参数和Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!