C# Linq In条款&;谓词构建
我有两张桌子报告和报告数据。 ReportData具有约束ReportID 如何编写linq查询以返回满足ReportData谓词条件的所有报表对象?SQL中的类似内容:C# Linq In条款&;谓词构建,c#,linq-to-sql,predicatebuilder,C#,Linq To Sql,Predicatebuilder,我有两张桌子报告和报告数据。 ReportData具有约束ReportID 如何编写linq查询以返回满足ReportData谓词条件的所有报表对象?SQL中的类似内容: SELECT * FROM Report as r Where r.ServiceID = 3 and r.ReportID IN (Select ReportID FROM ReportData WHERE JobID LIKE 'Something%') 这就是我构建谓词的方式: Expression<Func&l
SELECT * FROM Report as r
Where r.ServiceID = 3 and r.ReportID IN (Select ReportID FROM ReportData WHERE JobID LIKE 'Something%')
这就是我构建谓词的方式:
Expression<Func<ReportData, bool>> predicate = PredicateBuilder.True<ReportData>();
predicate = predicate.And(x => x.JobID.StartsWith(QueryConfig.Instance.DataStreamName));
var q = engine.GetReports(predicate, reportsDataContext);
reports = q.ToList();
但是,我得到了这个异常:“不支持的重载用于查询运算符‘Where’。”
更新
这修正了它:
var q = reportDC.Reports.AsExpandable().
Where(r => r.ReportDatas.Any(predicate.Compile()))
.Where(r => r.ServiceID.Equals(1));
查询
关于Linqkit
使用LinqKit时,有时需要在实体集合中调用
AsExpandable()
,并编译谓词表达式。查看此示例:):谢谢,这解决了我的问题。有一些小的调整。var q=reportDC.ReportDatas.Where(谓词)。选择(reportData=>reportData.Report)。其中(Report=>Report.ServiceID.Equals(1)).Distinct();
var q = from r in reportDC.Reports
where r.ServiceID.Equals(1)
where r.ReportDatas.Where(predicate.Compile()).Select(x => r.ReportID).Contains(r.ReportID)
select r;
return q;
}
var q = reportDC.Reports.AsExpandable().
Where(r => r.ReportDatas.Any(predicate.Compile()))
.Where(r => r.ServiceID.Equals(1));
ReportDatas
.Where( reportData => reportData.StartsWith( "Something%" ) &&
reportData.Report.Id ==3)
.Select( reportData => reportData.Report )
.Distinct()