我怎样才能使这个 LINQ to SQL where 条件更快?
How can I make this LINQ to SQL where clause with conditions faster?
我有以下 LINQ to SQL 查询,但我想知道是否有更快的方法来验证来自 post 操作的数据,然后再将它们添加到 where 子句中?例如:
bookFilter = Informations coming from the post action
int count = from b in _libraryContext.Book
join ba in _libraryContext.BookAuthor
on b.Id equals ba.BookId
where (!string.IsNullOrWhiteSpace(bookFilter.Name) ?
b.Name.Contains(bookFilter.Name.ToUpper()) : 1 == 1 )
where (!string.IsNullOrWhiteSpace(bookFilter.Decription) ?
b.Description.Contains(bookFilter.Description.ToUpper()) : 1 == 1)
where (bookFilter.BookId > 0 ? ba.BookId == bookFilter.BookId : 1 == 1)
return count;
我从来没有使用过这种类型的语法,所以我确定你是否可以这样做,但你当然可以用 LINQ 来做,并像这样逐步构建你的查询:
var query = _libraryContext.Set<Book>();
if(!string.IsNullOrWhiteSpace(bookFilter.Name))
{
query = query.Where(x => x.Name.Contains(bookFilter.Name.ToUpper()));
}
if(!string.IsNullOrWhiteSpace(bookFilter.Description))
{
query = query.Where(x => x.Description.Contains(bookFilter.Description.ToUpper()));
}
if(bookFilter.BookId > 0)
{
query = query.Where(x => x.BookId == bookFilter.Id);
}
return query.Count();
注意:我在这里省略了 JOIN,因为它似乎没有必要,但如果需要,您当然也可以使用此语法进行连接。
在 SQL 中,您可以使用 COALESCE 来默认忽略参数——如下所示:
SELECT *
FROM book b
JOIN BookAuthor ba on b.id = ba.Bookid
WHERE COALESCE(UPPER(name), UPPER(b.name)) = UPPER(b.name)
AND COALESCE(UPPER(description), UPPER(b.description)) = UPPER(b.description)
AND COALESCE(bookid, b.book.id) = b.bookid
推荐的模式是将类似的东西放在 SP 中,然后调用 SP。
您已经接受了@Akinzekeel 上面提供的解决方案。但是,如果您尝试使用单个 LINQ 查询,我会采用以下解决方案:
bookFilter = Informations coming from the post action
这里请注意bookFilter
必须是IQueryable!
int count = from b in _libraryContext.Book
join ba in _libraryContext.BookAuthor
on b.Id equals ba.BookId
where (b.Name.Contains(bookFilter.Name.ToUpper()) || string.IsNullOrWhiteSpace(bookFilter.Name)) &&
(b.Description.Contains(bookFilter.Description.ToUpper()) || string.IsNullOrWhiteSpace(bookFilter.Decription)) &&
(ba.BookId == bookFilter.BookId || bookFilter.BookId == 0 )
return count;
如果您在 SQL Profiler 中跟踪上面的代码,您会看到这将生成一个 SQL 查询;类似于 (OR IS NULL)
条件。
我有以下 LINQ to SQL 查询,但我想知道是否有更快的方法来验证来自 post 操作的数据,然后再将它们添加到 where 子句中?例如:
bookFilter = Informations coming from the post action
int count = from b in _libraryContext.Book
join ba in _libraryContext.BookAuthor
on b.Id equals ba.BookId
where (!string.IsNullOrWhiteSpace(bookFilter.Name) ?
b.Name.Contains(bookFilter.Name.ToUpper()) : 1 == 1 )
where (!string.IsNullOrWhiteSpace(bookFilter.Decription) ?
b.Description.Contains(bookFilter.Description.ToUpper()) : 1 == 1)
where (bookFilter.BookId > 0 ? ba.BookId == bookFilter.BookId : 1 == 1)
return count;
我从来没有使用过这种类型的语法,所以我确定你是否可以这样做,但你当然可以用 LINQ 来做,并像这样逐步构建你的查询:
var query = _libraryContext.Set<Book>();
if(!string.IsNullOrWhiteSpace(bookFilter.Name))
{
query = query.Where(x => x.Name.Contains(bookFilter.Name.ToUpper()));
}
if(!string.IsNullOrWhiteSpace(bookFilter.Description))
{
query = query.Where(x => x.Description.Contains(bookFilter.Description.ToUpper()));
}
if(bookFilter.BookId > 0)
{
query = query.Where(x => x.BookId == bookFilter.Id);
}
return query.Count();
注意:我在这里省略了 JOIN,因为它似乎没有必要,但如果需要,您当然也可以使用此语法进行连接。
在 SQL 中,您可以使用 COALESCE 来默认忽略参数——如下所示:
SELECT *
FROM book b
JOIN BookAuthor ba on b.id = ba.Bookid
WHERE COALESCE(UPPER(name), UPPER(b.name)) = UPPER(b.name)
AND COALESCE(UPPER(description), UPPER(b.description)) = UPPER(b.description)
AND COALESCE(bookid, b.book.id) = b.bookid
推荐的模式是将类似的东西放在 SP 中,然后调用 SP。
您已经接受了@Akinzekeel 上面提供的解决方案。但是,如果您尝试使用单个 LINQ 查询,我会采用以下解决方案:
bookFilter = Informations coming from the post action
这里请注意bookFilter
必须是IQueryable!
int count = from b in _libraryContext.Book
join ba in _libraryContext.BookAuthor
on b.Id equals ba.BookId
where (b.Name.Contains(bookFilter.Name.ToUpper()) || string.IsNullOrWhiteSpace(bookFilter.Name)) &&
(b.Description.Contains(bookFilter.Description.ToUpper()) || string.IsNullOrWhiteSpace(bookFilter.Decription)) &&
(ba.BookId == bookFilter.BookId || bookFilter.BookId == 0 )
return count;
如果您在 SQL Profiler 中跟踪上面的代码,您会看到这将生成一个 SQL 查询;类似于 (OR IS NULL)
条件。