【发布时间】:2021-01-25 20:28:39
【问题描述】:
我有以下 LINQ to SQL 查询,但我想知道在将它们添加到 where 子句之前是否有更快的方法来验证来自 post 操作的数据?例如:
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;
【问题讨论】:
-
在这种特殊情况下,您对“更好”的定义是什么?
-
让我们说更干净更快捷的方式?
-
“干净”是主观的,可以基于意见。但是,如果您谈论的是可以客观衡量的性能,您应该更新您的问题以指定您想要的。
-
@Zer0 我根据您的评论更新了标题。谢谢
-
@Kivo 这样的包罗万象的查询在 SQL 中是一种不好的做法。但在 LINQ 中根本不需要它们,因为您可以根据需要链接
.Where()调用
标签: c# linq linq-to-sql