Linq 的一个小查询
查询条件的很多种组合情况下,对于一个结果集的再次查询:
第一张写法就比较正常了.按道理说也是第一种写法比较符合正常的逻辑.可能就是出自一个差不多手的代码,而第二种呢?呵呵那就是比较的'考验'你的逻辑的严谨度了哦!嘻嘻
1>
var q = from c in _ticketsDataContext.V_ServiceResult
where c.MainAcountID == rec.MainAcountID
select new { c };
if (rec.StartDate != null && rec.EndDate != null)
{
q = q.Where(m => m.c.ServiceTime > rec.StartDate && m.c.ServiceTime < rec.EndDate.AddDays(1));
}
if (rec.SubAccountID > 0)
{
q = q.Where(m => m.c.SubAcountID == rec.SubAccountID);
}
if (rec.ServiceContent != "" && rec.ServiceContent != "所有")
{
q = q.Where(m => m.c.ServiceType == rec.ServiceContent);
}
if (rec.ContactType != "" && rec.ContactType != "所有")
{
q = q.Where(m => m.c.ContactType == rec.ContactType);
}
if (rec.CustomerName != "" && rec.CustomerName != null)
{
q = q.Where(m => m.c.CustomerName == rec.CustomerName);
}
TotalCount = q.Count();
q = q.Skip((PageIndex - 1) * PageSize).Take(PageSize);