繁体   English   中英

Linq to SQL-运行时有多个where子句

[英]Linq to SQL - Multiple where clauses at runtime

我正在尝试完成此操作,但是查询运行时仅使用我的第一个where子句。

对于.Net 3.5,这需要for,因此在4.0中的WhereIf不可用。

var query =
    from tb in dataContext.TableOne
    where tb.DateTimeCreated >= fromDate && 
        tb.DateTimeCreated <= toDate.AddDays(1) 
    select tb;

if (!string.IsNullOrEmpty(reference))
{
    query.Where(tb => tb.Reference = reference));
}
  if (!string.IsNullOrEmpty(reference))
        query = query.Where(tb => tb.Reference = reference));

尝试

只需做一个

var query = (from tb in dataContext.TableOne
                      where (tb.DateTimeCreated >= fromDate && tb.DateTimeCreated <= toDate.AddDays(1)) && (string.IsNullOrEmpty(reference) || tb.Reference == reference)
                      select tb
               );

或按照Ives所说:

query = query.Where(...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM