【发布时间】:2018-10-02 07:07:55
【问题描述】:
您好,我尝试按变量过滤结果 这是我尝试过的:
private IQueryable<Student> FilterStudents(IQueryable<Student> students, StudentFilter filter)
{
if (filter == null) return students; //No filter
var fromDate = new DateTime();
var toDate = new DateTime();
if (!string.IsNullOrEmpty(filter.DealsFrom))
{
fromDate = DateTime.Parse(filter.DealsFrom);
}
if (!string.IsNullOrEmpty(filter.DealsTo))
{
toDate = DateTime.Parse(filter.DealsTo);
}
students = students.Where(
s => ((!string.IsNullOrEmpty(filter.FirstName)) ? s.FirstName.Contains(filter.FirstName) : true
&& (!string.IsNullOrEmpty(filter.LastName)) ? s.LastName.Contains(filter.LastName) : true
&& (!string.IsNullOrEmpty(filter.Email)) ? s.Email.Contains(filter.Email) : true
&& (!string.IsNullOrEmpty(filter.Phone)) ? s.Phone.Contains(filter.Phone) : true
&& (!string.IsNullOrEmpty(filter.Comment)) ? s.Comment.Contains(filter.Comment) : true
&& (filter.UniversityId.HasValue) ? s.UniversityId == filter.UniversityId : true
&& (filter.StudentStatusId.HasValue) ? s.StudentStatusId == filter.StudentStatusId : true
&& (filter.FacultyId.HasValue) ? s.FacultyId == filter.FacultyId : true
&& (filter.CityId.HasValue) ? s.CityId == filter.CityId : true
&& (!string.IsNullOrEmpty(filter.degree)) ? s.degree == filter.degree : true
&& (!string.IsNullOrEmpty(filter.degyear)) ? s.degyear == filter.degyear : true
&& (filter.NeighborhoodId.HasValue) ? s.NeighborhoodId == filter.NeighborhoodId : true
&& (filter.DealsCityId.HasValue) ? s.DealsCityId == filter.DealsCityId : true
&& (filter.FavoriteBusinessId.HasValue) ? s.Favorites.Any(f => f.BusinessId == filter.FavoriteBusinessId) : true
&& (s.StudentDeals.Count > 0)
&& (
s.StudentDeals.Any(d =>
((!string.IsNullOrEmpty(filter.DealsFrom)) ? d.UsedDateTime >= fromDate : true)
&&
((!string.IsNullOrEmpty(filter.DealsTo)) ? d.UsedDateTime <= toDate : true)
)
)
&& (filter.DealsMin.HasValue) ? s.StudentDeals.Count >= filter.DealsMin : true
&& (filter.DealsMax.HasValue) ? s.StudentDeals.Count <= filter.DealsMax : true
&& (filter.CategoryId.HasValue) ? s.Categories.Any(c => c.Id == filter.CategoryId) : true
&& (filter.HasCardImage == true) ? s.CardImageFileName != null : true
&& (filter.HasCardImage == false) ? s.CardImageFileName == null : true
&& (filter.AppInstall == true) ? s.DeviceId != null : true
&& (filter.AppInstall == false) ? s.DeviceId == null : true
));
return students.OrderBy(s => s.FirstName);
}
但它返回所有记录...我输入了 fromdate 和 todate 并尝试输入 DealsMin 和 DealsMax 但相同,返回了所有记录。
我能做什么? tnx
编辑:
我试过喜欢这个:
private IQueryable<Student> FilterStudents(IQueryable<Student> students, StudentFilter filter)
{
if (filter == null) return students; //No filter
if (!string.IsNullOrEmpty(filter.FirstName))
{
students = students.Where(s => s.FirstName.Contains(filter.FirstName));
}
if (!string.IsNullOrEmpty(filter.LastName))
{
students = students.Where(s => s.LastName.Contains(filter.LastName));
}
if (!string.IsNullOrEmpty(filter.Email))
{
students = students.Where(s => s.Email.Contains(filter.Email));
}
if (!string.IsNullOrEmpty(filter.Phone))
{
students = students.Where(s => s.Phone.Contains(filter.Phone));
}
if (!string.IsNullOrEmpty(filter.Comment))
{
students = students.Where(s => s.Comment.Contains(filter.Comment));
}
if (filter.UniversityId.HasValue)
{
students = students.Where(s => s.UniversityId == filter.UniversityId);
}
if (filter.StudentStatusId.HasValue)
{
students = students.Where(s => s.StudentStatusId == filter.StudentStatusId);
}
if (filter.FacultyId.HasValue)
{
students = students.Where(s => s.FacultyId == filter.FacultyId);
}
if (filter.CityId.HasValue)
{
students = students.Where(s => s.CityId == filter.CityId);
}
if (!string.IsNullOrEmpty(filter.degree))
{
students = students.Where(s => s.degree == filter.degree);
}
if (!string.IsNullOrEmpty(filter.degyear))
{
students = students.Where(s => s.degyear == filter.degyear);
}
if (filter.NeighborhoodId.HasValue)
{
students = students.Where(s => s.NeighborhoodId == filter.NeighborhoodId);
}
if (filter.DealsCityId.HasValue)
{
students = students.Where(s => s.DealsCityId == filter.DealsCityId);
}
if (filter.FavoriteBusinessId.HasValue)
{
students = students.Where(s => s.Favorites.Any(f => f.BusinessId == filter.FavoriteBusinessId));
}
if (!string.IsNullOrEmpty(filter.DealsFrom))
{
var fromDate = DateTime.Parse(filter.DealsFrom);
students = students.Where(s => s.StudentDeals.Any(d => d.UsedDateTime >= fromDate));
}
if (!string.IsNullOrEmpty(filter.DealsTo))
{
var toDate = DateTime.Parse(filter.DealsTo);
students = students.Where(s => s.StudentDeals.Any(d => d.UsedDateTime <= toDate));
}
if (filter.DealsMin.HasValue)
{
students = students.Where(s => s.StudentDeals.Count >= filter.DealsMin);
}
if (filter.DealsMax.HasValue)
{
students = students.Where(s => s.StudentDeals.Count <= filter.DealsMax);
}
if (filter.CategoryId.HasValue)
{
students = students.Where(s => s.Categories.Any(c => c.Id == filter.CategoryId));
}
if (filter.HasCardImage == true)
{
students = students.Where(s => s.CardImageFileName != null);
}
if (filter.HasCardImage == false)
{
students = students.Where(s => s.CardImageFileName == null);
}
if (filter.AppInstall == true)
{
students = students.Where(s => s.DeviceId != null);
}
if (filter.AppInstall == false)
{
students = students.Where(s => s.DeviceId == null);
}
return students.OrderBy(s => s.FirstName);
}
但是现在当我输入 filter.dealsMin 和 filter.dealsMax 以及 toDate 和 FromDate 时,它没有在所有参数之间进行 AND...它只显示 fromDate 之后的记录并忽略 toDate...
【问题讨论】:
-
确保
condition ? first_expression : second_expression被方括号包围 =>(condition ? first_expression : second_expression)。这可能会解决您的问题。 -
如果您为此查询运行 SQL Profiler,您将看到生成的 SQL 语句,这将帮助您多年。我也同意@Paul 的观点,即您的“介于”日期逻辑中缺少括号。