我想知道如何在我的 linq 查询中添加一个动态 where 子句,其中我正在过滤的字段不是返回集的一部分。
询问:
(from p in db.Person
join c in db.Client on p.PersonId equals c.PersonId into c_join
from c in c_join.DefaultIfEmpty()
join pt in db.PersonType on p.PersonTypeId equals pt.PersonTypeId into pt_join
from pt in pt_join.DefaultIfEmpty()
join pl in db.Plan on c.ClientId equals pl.ClientId into pl_join
from pl in pl_join.DefaultIfEmpty()
join plt in db.PlanType on pl.PlanTypeId equals plt.PlanTypeId into plt_join
from plt in plt_join.DefaultIfEmpty()
orderby pt.PersonTypeDescription ascending , p.LastName ascending
select new ExtendedPersonSearch
{
PersonId = p.PersonId,
FirstName = p.FirstName,
LastName = p.LastName,
MiddleName = p.MiddleName,
Aka = p.Aka,
Sin = p.Sin,
PersonTypeId = pt.PersonTypeId,
PersonTypeDescription = pt.PersonTypeDescription,
ClientId = (Int32?) c.ClientId
}).Distinct();
相关课程:
public class ExtendedPersonSearch
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Aka { get; set; }
public string Sin { get; set; }
public int PersonTypeId { get; set; }
public string PersonTypeDescription { get; set; }
public int? AddressId { get; set; }
public string Street1 { get; set; }
public int? ClientId { get; set; }
但是,我想对不属于 ExtendedPersonSearch 类的 pl.PlanId 进行过滤。
这些其他类型的动态 where 子句适用于返回集中的项目:
if (fd.Contains("txtSearchFirstName"))
{
var searchFirstName = form[fd];
ViewData["searchFirstName"] = searchFirstName;
if (searchFirstName != "")
qryAllPerson = qryAllPerson.Where(sp => sp.FirstName.Contains(searchFirstName));
}
没有根据用户是否输入计划 ID 来再次编写查询以获取此查询:
qryAllPerson = (from p in db.Person
join c in db.Client on p.PersonId equals c.PersonId into c_join
from c in c_join.DefaultIfEmpty()
join pt in db.PersonType on p.PersonTypeId equals pt.PersonTypeId into pt_join
from pt in pt_join.DefaultIfEmpty()
join pl in db.Plan on c.ClientId equals pl.ClientId into pl_join
from pl in pl_join.DefaultIfEmpty()
join plt in db.PlanType on pl.PlanTypeId equals plt.PlanTypeId into plt_join
from plt in plt_join.DefaultIfEmpty()
orderby pt.PersonTypeDescription ascending, p.LastName ascending
where pl.PlanId == searchPlanId
select new ExtendedPersonSearch
{
PersonId = p.PersonId,
FirstName = p.FirstName,
LastName = p.LastName,
MiddleName = p.MiddleName,
Aka = p.Aka,
Sin = p.Sin,
PersonTypeId = pt.PersonTypeId,
PersonTypeDescription = pt.PersonTypeDescription,
ClientId = (Int32?)c.ClientId
}).Distinct();
有没有一种方法可以让我只添加 PlanId 的 where 子句,而不必再次输入整个查询?