我正在尝试创建以下LINQ方法。问题似乎是第二个WHERE子句。我收到此错误 - >
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<MatrixReloaded.Data.CMO.tblWorkerHistory>' to 'bool'
我还有&&
与WHERE
,但我遇到了类似的错误。除了tblWorkerHistories
之外,我不需要EndDate
中的任何内容。
两张表格之间存在多对多关系,EnrollmentID
在 FK 两者之间。
public static DataTable GetCurrentWorkersByEnrollmentID(int enrollmentID)
{
using (var context = CmoDataContext.Create())
{
context.Log = Console.Out;
var currentWorkers = from enrollment in context.tblCMOEnrollments
where enrollment.EnrollmentID == enrollmentID
where enrollment.tblWorkerHistories.Where(a => a.EndDate == null || a.EndDate > DateTime.Now)
select
new
{
enrollment.CMONurseID,
enrollment.CMOSocialWorkerID,
SupportWorkerName = enrollment.tblSupportWorker.FirstName + " " + enrollment.tblSupportWorker.LastName,
SupportWorkerPhone = enrollment.tblSupportWorker.Phone
};
return currentWorkers.CopyLinqToDataTable();
}
}
答案 0 :(得分:4)
我怀疑你的意思是.Any
而不是.Where
在子查询中;最外面的.Where
(即第二个where
)需要一个谓词表达式,但是你的表达式选择 - 尝试:
where enrollment.tblWorkerHistories.Any(
a => a.EndDate == null || a.EndDate > DateTime.Now)
答案 1 :(得分:4)
这是问题所在:
where enrollment.tblWorkerHistories.Where(/* stuff */)
Where
返回一个序列...而你需要一些会返回一个布尔值的东西。你想用嵌入式Where
子句做什么?
正如Marc所说,它可能你需要Any
来代替Where
......但如果你能解释你想要做什么,这将使你更容易帮助你。请注意,Any
会返回布尔值,而不是序列。
Any
确实是您想要的:
var currentWorkers = from enrollment in context.tblCMOEnrollments
where enrollment.EnrollmentID == enrollmentID
where enrollment.tblWorkerHistories.Any
(a => a.EndDate == null || a.EndDate > DateTime.Now)
select ...