2013-03-08 40 views
2

我想對多個屬性執行搜索條件,但是,我遇到問題。如果某些屬性爲空或空這不是搜索條件的一部分,請幫助me..here是下面的代碼:關於多個屬性的搜索條件c#

public List<AccountDto> getSearchedAccount(int accountid,int userid,String holdername,String type,double balance,String status) 
{ 
    List<AccountDto> results = new List<AccountDto>(); 
    for (int i = 0; i < list.Count; i++) 
    { 
     AccountDto dto = (AccountDto)list[i]; 
     if ((dto.Accountid == accountid) && (dto.Userid==userid) && (dto.Holdername.Equals(holdername)) && (dto.Balance == balance) && (dto.Status.Equals(status))) 
     { 
      results.Add(dto); 
     } 

    } 

    return results; 
} 

請告訴我正確的if語句是某些字段爲空或空時不進入搜索標準。

+0

您是否擁有穩定數量的'Properties'?否則這個解決方案最終會炸燬你的代碼。 – 2013-03-08 15:17:05

+0

yes屬性是穩定的 – 2013-03-08 15:20:15

+0

您可能還想將基本類型參​​數'accountid','userid'和'balance'設置爲[可空屬性](http://msdn.microsoft.com/en-us/library /b3h38hb0.aspx)(例如'int?'或'double?'),所以你可以傳遞'null'而不是像'0'這樣的模糊值。 – mellamokb 2013-03-08 15:26:56

回答

4

它會是這樣的:

if (accountid < 0 || dto.Accountid == accountid) 
    && ... 
    && (string.IsNullOrEmpty(holdername) || dto.Holdername.Equals(holdername)) 
    && ...) 
    { 
     results.Add(dto); 
    } 

介紹的或者如果該值未設置或對比的事項是真實的每個條件。例如,如果持有者名稱爲空或空,則不會評估持有者的等於名稱。

+0

非常感謝... – 2013-03-08 15:39:13

3

你爲什麼不爲此創建一個方法?在該方法中,您可以檢查該屬性是否爲null或空或其他。

private bool CheckAccount(AccountDto dto, int accountid, int userid, String holdername, string type, double balance, String status){ 
bool isTrue = true; 
... 
if(holdername != null){ 
    if(!dto.Holdername.Equals(holdername)) 
      return false; 
} 
... 
return true; //all properties are true 
} 
0

您可以在搜索條件構建聚合過濾器。我認爲下面的帖子與你正在尋找的是一樣的。 Try This

0

我可能會覈對默認(類型)string.IsNullOrEmpty(...)

所以,你會可能有:

public List<AccountDto> getSearchedAccount(int accountid, int userid, string holdername, string type, double balance, string status) 
{ 
    var results = new List<AccountDto>(); 

    for (int i = 0; i < list.Count; i++) 
    { 
     AccountDto dto = (AccountDto)list[i]; 

     if (accountid != default(int) && accountid != dto.Accountid) 
      continue; 
     if (userid != default(int) && userid != dto.Userid) 
      continue; 
     if (!string.IsNullOrEmpty(holdername) && !holdername.Equals(dto.Holdername)) 
      continue; 
     if (!string.IsNullOrEmpty(type) && !type.Equals(dto.Type)) 
      continue; 
     if (balance != default(double) && balance != dto.Balance) 
      continue; 
     if (!string.IsNullOrEmpty(status) && !status.Equals(dto.Status)) 
      continue; 

     results.Add(dto); 
    } 

    return results; 
} 

或利用的表達樹

public List<AccountDto> getSearchedAccount(int accountid, int userid, string holdername, string type, double balance, string status) 
{ 
    IQueryable<AccountDto> query = list.AsQueryable(); 

    if (accountid != default(int)) 
     query = query.Where(i => i.Accountid.Equals(accountid)); 
    if (userid != default(int)) 
     query = query.Where(i => i.Userid.Equals(userid)); 
    if (!string.IsNullOrEmpty(holdername)) 
     query = query.Where(i => i.Holdername.Equals(holdername)); 
    if (!string.IsNullOrEmpty(holdername)) 
     query = query.Where(i => i.Type.Equals(type)); 
    if (balance != default(double)) 
     query = query.Where(i => i.Balance.Equals(balance)); 
    if (!string.IsNullOrEmpty(holdername)) 
     query = query.Where(i => i.Status.Equals(status)); 

    return query.Select(i => i).ToList<AccountDto>(); 
} 

夫婦的想法

  • 我敢肯定,您的貨幣價值小數不會加倍。
  • 創建一個對象來表示,這樣你不必每次你決定添加一個新的領域

    公開名單getSearchedAccount(AccountSearchCritera標準)時間改變方法簽名的標準{...}

0
/*** Answer with the comments in code ***/ 
// don't create a class to represent your criteria 
// Dictionary is enough for use and supported by Linq 
// don't use a lot of `&&` or `if`; one `if` is enough 
// all the rules is in deferred execution, only executes when it really needs 
// evaluated and the order to put where clauses matters, don't make it changed 
// it performs like short-circuit evaluation 

/// <summary> 
/// declared as partial for easily coexists with original code 
/// if not used(e.g already declared) then not paste to file 
/// </summary> 
partial class AccountDto /* known members */ { 
    public int Accountid; 
    public int Userid; 
    public String Holdername; 
    public int Balance; 
    public String Status; 
} 

/// <summary> 
/// declared as partial for easily coexists with original code 
/// if getSearchedAccount is declared with another class name 
/// then just rename the partial class to that name and remove 
/// all `static`(if that class is non-static) 
/// the class initializer, then become constructor; remember to 
/// match the name of class and constructor 
/// </summary> 
partial class AccountDto { 
    /// <summary> 
    /// declare as static for this demo; 
    /// not necessarily be static if it's declared in another 
    /// class where list is declared 
    /// </summary> 
    public static List<AccountDto> getSearchedAccount(
     int accountid, int userid, 
     String holdername, String type, 
     double balance, 
     String status 
     ) { 
     var results=new List<AccountDto>(); 

     // make a copy of IgnoreRules and clear; equivalent to 
     // var matchRules=new Dictionary<String, Func<AccountDto, bool>>(); 
     // IgnoreRules is not changed with these two statement 
     // just prevent to see too many angle braces 
     var matchRules=IgnoreRules.ToDictionary(x => x.Key, x => x.Value); 
     matchRules.Clear(); 

     // the parameters only known in this method thus can only added here 
     matchRules.Add("accountid", x => accountid==x.Accountid); 
     matchRules.Add("userid", x => userid==x.Userid); 
     matchRules.Add("holdername", x => holdername==x.Holdername); 
     matchRules.Add("balance", x => balance==x.Balance); 
     matchRules.Add("status", x => status==x.Status); 

     for(int i=0; i<list.Count; i++) { 
      var dto=(AccountDto)list[i]; 

      if((from ignoreRule in IgnoreRules 
       from matchRule in matchRules 
       where ignoreRule.Key==matchRule.Key 
       where !ignoreRule.Value(dto) 
       select matchRule.Value(dto)).All(x => x)) 
       results.Add(dto); 
     } 

     return results; 
    } 

    /// <summary> 
    /// criteria for `don't test for matching` 
    /// </summary> 
    public static Dictionary<String, Func<AccountDto, bool>> IgnoreRules { 
     get; 
     set; 
    } 

    /// <summary> 
    /// use class initializer to add common IgnoreRules 
    /// </summary> 
    static AccountDto() { 
     IgnoreRules=new Dictionary<String, Func<AccountDto, bool>>(); 
     IgnoreRules.Add("accountid", x => 0==x.Accountid); 
     IgnoreRules.Add("userid", x => 0==x.Userid); 
     IgnoreRules.Add("holdername", x => String.IsNullOrEmpty(x.Holdername)); 
     IgnoreRules.Add("balance", x => 0==x.Balance); 
     IgnoreRules.Add("status", x => String.IsNullOrEmpty(x.Status)); 
    } 

    /// <summary> 
    /// declare as static for this demo; 
    /// not necessarily be static if it's declared in another 
    /// class where getSearchedAccount is declared 
    /// </summary> 
    public static List<AccountDto> list=new List<AccountDto>(); 
}