2

我想对多个属性执行搜索条件,但是我遇到了问题。如果某些属性为空或不属于搜索条件的 null,请帮助我..这是以下代码:

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 语句是否某些字段为空或空,而这些字段不在搜索条件中。

4

6 回答 6

4

它会是这样的:

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

如果未设置值或比较很重要,则为每个条件引入一个或。例如,如果 holdername 为 null 或为空,则不会评估 Holdername 的 Equals。

于 2013-03-08T15:21:33.237 回答
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
}
于 2013-03-08T15:22:23.510 回答
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>();
}
于 2013-03-08T17:10:39.830 回答
0

我认为这可能是您正在寻找的:C# Coalesce

于 2013-03-08T15:22:32.607 回答
0

您可以在搜索条件中构建聚合过滤器。我认为下面的帖子与您正在寻找的内容相同。尝试这个

于 2013-03-08T15:30:55.693 回答
0

我可能会检查default(type)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 条件) { ... }

于 2013-03-08T15:46:09.813 回答