LINQ to SQL - WHERE 子句过滤
LINQ to SQL - WHERE clause filtered
为什么 where
条件没有应用在下面的代码中?当我搜索时,结果没有被过滤。当我调试时,searchString
确实有一个值并且执行是通过 _data.Where
子句进行的,但它没有返回过滤后的结果。
我哪里做错了?
IQueryable<UserViewModel> _data = (
from a in context.aspnet_Users
join b in context.aspnet_Membership on a.UserId equals b.UserId
where b.IsApproved == true
select new UserViewModel
{
UserId = a.UserId,
UserName = a.UserName,
FirstName = a.FirstName,
LastName = a.LastName,
EmailAddress = b.Email
})
.OrderBy(a => a.FirstName)
.ThenBy(b => b.LastName);
if (!String.IsNullOrEmpty(searchString))
{
_data = _data
.Where(x => x.FirstName.Contains(searchString))
.Where(y => y.UserName.Contains(searchString))
.Where(y => y.LastName.Contains(searchString))
.Where(y => y.EmailAddress.Contains(searchString));
}
第二个查询的编写方式检查所有 过滤字段以搜索字符串开头的记录,即对于 FirstName and[=19= 的用户] LastName and UserName and Email 都以相同的字符串开头。
我认为您想要的是检查这些字段是否以搜索字符串开头的查询,例如:
_data = _data.Where(x => x.FirstName.Contains(searchString) ||
x.UserName.Contains(searchString) ||
x.LastName.Contains(searchString) ||
x.EmailAddress.Contains(searchString));
我怀疑这 "filtering" 比您预期的要多得多:
_data = _data.Where(x => x.FirstName.Contains(searchString))
.Where(y => y.UserName.Contains(searchString))
.Where(y => y.LastName.Contains(searchString))
.Where(y => y.EmailAddress.Contains(searchString));
这要求 searchString
存在于 所有四个 字段中。因此,如果您搜索 "David" 那么它将无法找到我的记录,因为我的 LastName
不包含该字符串。
也许您想使用逻辑 or 而不是逻辑 and?像这样:
_data = _data.Where(x => x.FirstName.Contains(searchString) ||
x.UserName.Contains(searchString) ||
x.LastName.Contains(searchString) ||
x.EmailAddress.Contains(searchString));
为什么 where
条件没有应用在下面的代码中?当我搜索时,结果没有被过滤。当我调试时,searchString
确实有一个值并且执行是通过 _data.Where
子句进行的,但它没有返回过滤后的结果。
我哪里做错了?
IQueryable<UserViewModel> _data = (
from a in context.aspnet_Users
join b in context.aspnet_Membership on a.UserId equals b.UserId
where b.IsApproved == true
select new UserViewModel
{
UserId = a.UserId,
UserName = a.UserName,
FirstName = a.FirstName,
LastName = a.LastName,
EmailAddress = b.Email
})
.OrderBy(a => a.FirstName)
.ThenBy(b => b.LastName);
if (!String.IsNullOrEmpty(searchString))
{
_data = _data
.Where(x => x.FirstName.Contains(searchString))
.Where(y => y.UserName.Contains(searchString))
.Where(y => y.LastName.Contains(searchString))
.Where(y => y.EmailAddress.Contains(searchString));
}
第二个查询的编写方式检查所有 过滤字段以搜索字符串开头的记录,即对于 FirstName and[=19= 的用户] LastName and UserName and Email 都以相同的字符串开头。
我认为您想要的是检查这些字段是否以搜索字符串开头的查询,例如:
_data = _data.Where(x => x.FirstName.Contains(searchString) ||
x.UserName.Contains(searchString) ||
x.LastName.Contains(searchString) ||
x.EmailAddress.Contains(searchString));
我怀疑这 "filtering" 比您预期的要多得多:
_data = _data.Where(x => x.FirstName.Contains(searchString))
.Where(y => y.UserName.Contains(searchString))
.Where(y => y.LastName.Contains(searchString))
.Where(y => y.EmailAddress.Contains(searchString));
这要求 searchString
存在于 所有四个 字段中。因此,如果您搜索 "David" 那么它将无法找到我的记录,因为我的 LastName
不包含该字符串。
也许您想使用逻辑 or 而不是逻辑 and?像这样:
_data = _data.Where(x => x.FirstName.Contains(searchString) ||
x.UserName.Contains(searchString) ||
x.LastName.Contains(searchString) ||
x.EmailAddress.Contains(searchString));