linq 动态 多条件 查询
var Check = (from S in DC.Study
join P in DC.Patient on S.PatientID equals P.pID
join R in DC.Result on S.StuID equals R.sID
where S.rMod == _mod //&& (beginDate <= [color=#0000FF]_Date&& endDate <= _Date)[/color]
select new
{
姓名 = P.pName,
性别 = P.SexTitle,
年龄 = DateTime.Now.Year - Convert.ToDateTime(P.birdat).Year,
是否打印 = S.IsPrinted,
}).Distinct();
其中的_Date是一个从下拉列表动态获取的时间类型。但在linq中不支持直接的拼接,请问有什么办法可以解决吗。
------解决方案--------------------你意思 _Date是string 类型的
那用Convert.ToDateTime()转换一下不就OK了
var Check = (from S in DC.Study
join P in DC.Patient on S.PatientID equals P.pID
join R in DC.Result on S.StuID equals R.sID
where S.rMod == _mod && (S.beginDate <= Convert.ToDateTime(_Date) && S.endDate <= Convert.ToDateTime(_Date))
select new
{
姓名 = P.pName,
性别 = P.SexTitle,
年龄 = DateTime.Now.Year - Convert.ToDateTime(P.birdat).Year,
是否打印 = S.IsPrinted,
}).Distinct();
------解决方案--------------------C# code
var Check = (from S in DC.Study
join P in DC.Patient on S.PatientID equals P.pID
join R in DC.Result on S.StuID equals R.sID
where S.rMod == _mod
select new {P,S};
if(!string.IsNullOrEmpty(_Date))
{
Check=Check.Where(c=>c.S.beginDate <= _Date && c.S.endDate <=_Date);
}
var result=Check.Select(c=>new
{
姓名 = c.P.pName,
性别 = c.P.SexTitle,
年龄 = DateTime.Now.Year - Convert.ToDateTime(c.P.birdat).Year,
是否打印 = c.S.IsPrinted,
}).Distinct();