使用Linq或存储过程过滤多个参数

时间:2019-03-10 09:13:20

标签: sql linq search stored-procedures

有没有一种更好的方法可以在不使用多个if语句的情况下使用多个参数(仅带值的参数)实现过滤:

IQueryable<vw_GetAllSalesInvoice> query = null;

if (string.IsNullOrEmpty(customerName) && string.IsNullOrEmpty(invoiceNumber) && status == 0 && !fromDate.HasValue && !toDate.HasValue)
    query = from x in _context.vw_GetAllSalesInvoice select x;

if (!string.IsNullOrEmpty(customerName) && string.IsNullOrEmpty(invoiceNumber) && status == 0 && !fromDate.HasValue && !toDate.HasValue)
    query = from x in _context.vw_GetAllSalesInvoice 
            where x.CustomerName.Contains(customerName) 
            select x;

if (!string.IsNullOrEmpty(invoiceNumber) && string.IsNullOrEmpty(customerName) && status == 0 && !fromDate.HasValue && !toDate.HasValue)
    query = from x in _context.vw_GetAllSalesInvoice 
            where x.InvoiceNumber.Contains(invoiceNumber) 
            select x;

if (status != 0 && string.IsNullOrEmpty(invoiceNumber) && string.IsNullOrEmpty(customerName) && !fromDate.HasValue && !toDate.HasValue)
    query = from x in _context.vw_GetAllSalesInvoice 
            where x.Status == status 
            select x;

if (fromDate.HasValue && toDate.HasValue && status == 0 && string.IsNullOrEmpty(invoiceNumber) && string.IsNullOrEmpty(customerName))
    query = from x in _context.vw_GetAllSalesInvoice 
            where x.InvoiceDate >= fromDate && x.InvoiceDate <= toDate 
            select x;

if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(invoiceNumber) && status == 0 && !fromDate.HasValue && !toDate.HasValue)
    query = from x in _context.vw_GetAllSalesInvoice 
            where x.CustomerName.Contains(customerName) && x.InvoiceNumber.Contains(invoiceNumber) 
            select x;

我相信将会有一种更简单的方法来实现这一目标。

2 个答案:

答案 0 :(得分:0)

您可以对x&&组合使用变量和||的条件,并按以下方式构建查询:

IQueryable<vw_GetAllSalesInvoice> query = from x in _context.vw_GetAllSalesInvoice where 
    (string.IsNullOrEmpty(customerName) || x.CustomerName.Contains(customerName)) && 
    (string.IsNullOrEmpty(invoiceNumber) || x.InvoiceNumber.Contains(invoiceNumber)) && 
    (status == 0 || x.Status == status) && 
    ((!fromDate.HasValue || !toDate.HasValue) && x.InvoiceDate >= fromDate && x.InvoiceDate <= toDate)
    select x;

由于||短路,因此只有在x.CustomerName.Contains(customerName)不为null且不为空字符串的情况下,customerName才会被求值-其余条件在所有其他条件下都成立。

答案 1 :(得分:0)

您可以像这样级联查询:

IQueryable<vw_GetAllSalesInvoice> query = _context.vw_GetAllSalesInvoice;

if (!string.IsNullOrEmpty(customerName)
    query = from x in query
            where x.CustomerName.Contains(customerName) 
            select x;

if (!string.IsNullOrEmpty(invoiceNumber))
    query = from x in query 
            where x.InvoiceNumber.Contains(invoiceNumber)
            select x;

if (status != 0)
    query = from x in query
            where x.Status == status 
            select x;

if (fromDate.HasValue)
    query = from x in query 
            where x.InvoiceDate >= fromDate && x.InvoiceDate <= toDate 
            select x;

如果您不介意使用方法语法,它甚至可以更短:

IQueryable<vw_GetAllSalesInvoice> query = _context.vw_GetAllSalesInvoice;

if (!string.IsNullOrEmpty(customerName)
    query = query.Where(x => x.CustomerName.Contains(customerName));

if (!string.IsNullOrEmpty(invoiceNumber))
    query = query.Where(x => x.InvoiceNumber.Contains(invoiceNumber));

if (status != 0)
    query = query.Where(x => x.Status == status);

if (fromDate.HasValue)
    query = query.Where(x => x.InvoiceDate >= fromDate && 
                             x.InvoiceDate <= toDate);