0

我有两张桌子。报告报告数据。ReportData 有一个约束 ReportID。

如何编写我的 linq 查询以返回满足 ReportData 谓词条件的所有 Report 对象?在 SQL 中是这样的:

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();

这是我目前的查询结构:

    public override IQueryable<Report> GetReports(Expression<Func<ReportData, bool>> predicate, LLReportsDataContext reportDC)
    {
        if (reportDC == null) throw new ArgumentNullException("reportDC");

        var q = reportDC.ReportDatas.Where(predicate).Where(r => r.ServiceID.Equals(1)).Select(r => r.Report);
        return q;
    }

我也尝试过执行以下操作: public override IQueryable GetReports(Expression> predicate, LLReportsDataContext reportDC) { if (reportDC == null) throw new ArgumentNullException("reportDC");

        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;
    }

但是,我得到了这个异常:“用于查询运算符'Where'的不支持的重载。”

更新 这修复了它:

        var q = reportDC.Reports.AsExpandable().
            Where(r => r.ReportDatas.Any(predicate.Compile()))
            .Where(r => r.ServiceID.Equals(1));
4

1 回答 1

1

询问

ReportDatas
.Where( reportData => reportData.StartsWith( "Something%" ) &&
     reportData.Report.Id ==3)
.Select( reportData => reportData.Report )
.Distinct()

关于 LinqKit

在使用 LinqKit 时,有时需要调用AsExpandable()实体集合并编译谓词表达式。看这个例子:): how-to-use-predicate-builder-with-linq2sql-and-or-operator

于 2010-05-18T13:23:15.107 回答