2010-07-07 25 views
2

我有以下方法:C#处理所有可能的零和非NULL值

public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) 
{ 
    return db.Profiles.Where(p => p.CountryFrom.CountryName.Equals(CountryFrom, 
     StringComparison.OrdinalIgnoreCase)); 
} 

什么是写的最好的方式其中条款,将滤波器的输入参数的所有可能的组合在一声明:

BOTH CountryFrom和CountryLoc = NULL

只有CountryFrom空

Only CountryLoc null

BOTH CountryFrom和CountryLoc不为空。

很快..我需要按年龄,性别,职业筛选配置文件..你的名字。

我正试图找到一种方法在C#中高效地编写它。我知道如何在TSQL中以干净的方式做到这一点。我希望我知道的方式。感谢迄今为止的所有回应。

+1

[c#.net 4.0:可为空的输入字符串参数和lambda表达式。](http:// stackoverflow。com/questions/3191687/c-net-4-0-nullable -input-string-parameter-and-the-lambda-expressions) – 2010-07-07 16:19:42

+0

请考虑更新你以前的问题,并要求更清楚,而不是创建一个副本。 – 2010-07-07 16:20:46

+0

是的,看起来非常相似,尽管你的三个问题中的第一个是不同的。尽量避免任何看起来像双重张贴的东西。 – Noldorin 2010-07-07 16:21:18

回答

2

一个好的旧的二进制XNOR操作将在这里做的伎俩:

db.Profiles.Where(p => !(p.CountryFrom == null^p.CountryTo == null)) 

它有效地等同2个布尔值,但对我来说,更直接,少一些令人费解甚至比写作((p.CountryFrom == null) == (p.CountryTo == null))

0

我不会把这个优雅:

public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) 
{ 
    return db.Profiles.Where(p => 
     { 
      p.ContryFrom != null && 
      p.CountryFrom.CountryName != null && 
      p.CountryFrom.CountryName.Equals(CountryFrom, StringComparison.OrdinalIgnoreCase) 
     }); 
} 
0

我会用这个简单的LINQ语法...

BOTH CountryFrom和CountryLoc = NULL

var result = from db.Profiles select p 
      where (p.CountryFrom == null) && (p.CountryLoc == null) 
      select p 

只有CountryFrom空

var result = from db.Profiles select p 
      where (p.CountryFrom == null) && (p.CountryLoc != null) 
      select p 

Only CountryLoc null

var result = from db.Profiles select p 
      where (p.CountryFrom != null) && (p.CountryLoc == null) 
      select p 

BOTH CountryFrom and CountryLoc are not null。

var result = from db.Profiles select p 
      where (p.CountryFrom != null) && (p.CountryLoc != null) 
      select p 

希望它可以帮助;-)

+0

我想你误解了这个问题。他希望所有结果都通过单个查询返回。 – Noldorin 2010-07-07 16:42:19

0

我赞成不试图补习班太多逻辑成LINQ表达式来的。为什么不在这样的单独函数中包含比较逻辑?

编辑:我提供了MatchesCountry函数的示例实现。

class Example 
{ 
    public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) 
    { 
     return db.Profiles.Where(p => p.MatchesCountry(CountryFrom, CountryLoc)); 
    } 
} 

public static class ProfileExtensions 
{ 
    public static bool MatchesCountry(this Profile profile, string CountryFrom, string CountryLoc) 
    { 
     // NOTE: Your comparison logic goes here. Below is an example implementation 

     // if the CountryFrom parameter was specified and matches the profile's CountryName property 
     if(!string.IsNullOrEmpty(CountryFrom) && string.Equals(profile.CountryName, CountryFrom, StringComparison.OrdinalIgnoreCase)) 
      return true; // then a match is found 

     // if the CountryLoc parameter was specified and matches the profile's CountryCode property 
     if (!string.IsNullOrEmpty(CountryLoc) && string.Equals(profile.CountryCode, CountryLoc, StringComparison.OrdinalIgnoreCase)) 
      return true; // then a match is found 

     // otherwise, no match was found 
     return false; 
    } 
} 
+0

你能否给我提供一些示例代码,介绍单独函数中的内容。 – 2010-07-07 20:50:31

+0

此代码可能不会同时匹配CountryFrom和CountryLoc ..: - ( – 2010-07-08 00:28:14

+0

好吧,如果您需要处理CountryFrom和CountryLoc为null或非null的所有可能组合,那么您只需编写MatchesCountry的逻辑以满足您的需求我的答案旨在提供一种方法来编写干净/可理解的where子句,将“国家匹配”逻辑分离为其自己的函数也允许在其他地方重用,以防有用。将逻辑匹配到它自己的函数中,但是您可以编写自己的逻辑,但最简单的方法就是不用将其填充到lambda表达式中。 – 2010-07-08 13:08:10

0

我可能失去了一些东西,但写的,你的运营商的结合将要么让通过或没有价值的所有值通过取决于是否使用||或& &将它们组合在一起。