简化C#中布尔检查的最佳方法,其中有很多不同的可能性

时间:2019-03-02 07:13:36

标签: c# linq boolean

如何减少此代码?我有transferId,weekId和lineId,我希望能够根据url参数是否给定来查询数据库。

示例:

route / Query?transferId = 5325&lineId = 10

route / Query?weekdId = 11

以此类推...

public async Task<ICollection<TransferEntity>> GetQueryAsync(string transferId, int? weekId, int? lineId)
    {
        if (Helpers.NullOrWhiteSpaceAll(transferId, weekId.ToString(), lineId.ToString()))
        {
            return null;
        }
        else if (!Helpers.NullOrWhiteSpaceAny(transferId, weekId.ToString(), lineId.ToString()))
        {
            return await _context.Transfers.Where(x => x.TransferId == transferId && x.WeekId == weekId && x.LineId == lineId).ToListAsync();
        }
        if (Helpers.NullOrWhiteSpace(transferId))
        {
            if (!Helpers.NullOrWhiteSpaceAny(weekId, lineId))
            {
                return await _context.Transfers.Where(x => x.WeekId == weekId && x.LineId == lineId).ToListAsync();
            }
            else if (!Helpers.NullOrWhiteSpace(weekId))
            {
                return await _context.Transfers.Where(x => x.WeekId == weekId).ToListAsync();
            }
            else
            {
                return await _context.Transfers.Where(x => x.LineId == lineId).ToListAsync();
            }
        }
        else if (Helpers.NullOrWhiteSpace(weekId))
        {
            if (!Helpers.NullOrWhiteSpaceAny(transferId, lineId.ToString()))
            {
                return await _context.Transfers.Where(x => x.TransferId == transferId && x.LineId == lineId).ToListAsync();
            }
            else if (!Helpers.NullOrWhiteSpace(transferId))
            {
                return await _context.Transfers.Where(x => x.TransferId == transferId).ToListAsync();
            }
            else
            {
                return await _context.Transfers.Where(x => x.LineId == lineId).ToListAsync();
            }
        }
        else
        {
            return await _context.Transfers.Where(x => x.TransferId == transferId && x.WeekId == weekId).ToListAsync();
        }
    }

3 个答案:

答案 0 :(得分:3)

Linq-to-SQL将多个Where子句组合成一个查询-因此您可以根据需要简单地添加它们并在最后执行:

var transfers = _context.Transfers;

if (!Helpers.NullOrWhiteSpaceAny(transferId))
{
    transfers = transfers.Where(x => x.TransferId == transferId);
}
if (weekId.HasValue)
{
    transfers = transfers.Where(x => x.WeekId == weekId);
}

return _context.Transfers == transfers? null : // did not add anything
    await  transfers.ToListAsync();

答案 1 :(得分:1)

谢谢您的帮助,但这是一种更好的方法。 我有transferId,weekId,lineId 我只想按非null或空格的内容搜索 我意识到可以忽略空格,因为我得到的输入永远不会是空格

这是我的解决方法

public async Task<ICollection<TransferEntity>> GetQueryAsync(string transferId, int? weekId, int? lineId) =>
        await _context.Transfers.Where(x => (transferId == null || x.TransferId == transferId) && (weekId == null || x.WeekId == weekId) && (lineId == null || x.LineId == lineId)).ToListAsync();

答案 2 :(得分:-1)

尝试一下。首先,对所有奇异参数及其可能的组合进行标记枚举:

public enum ParamSet { none = 0, transferId = 1, weekId = 2, lineId =4,
  // composite values
  transferId_weekId = 3
}

此处第一行是各个参数标志:如果param不为null,则设置该标志,否则不设置。第二行是允许的参数组合。例如,转账和周ID。是3,因为1 + 2;

然后,分析当前参数值:

ParamSet currentParams = (string.IsNullOrEmpty(transferId) ? ParamSet.none : ParamSet.transferId) | (weekId == null ? ParamSet.none : ParamSet.weekId) | (lineId == null ? ParamSet.none : ParamSet.lineId);

最后,获得一个switch

      switch(currentParams)
  {
    case ParamSet.transferId_weekId: break;
    case ParamSet.transferId: break;
    default: throw new Exception("Unsoppurted param config");
  }