0

2つのテーブルがあります。ReportReportData。ReportDataには制約ReportIDがあります。

ReportDataの述語条件が満たされているすべてのReportオブジェクトを返すようにlinqクエリを作成するにはどうすればよいですか?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;
    }

次のことも試してみました。publicoverrideIQueryableGetReports(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()

AboutLinqKit

LinqKitを使用する場合AsExpandable()、エンティティコレクションを呼び出して、述語式をコンパイルする必要がある場合があります。この例を参照してください:):how-to-use-predicate-builder-with-linq2sql-and-or-operator

于 2010-05-18T13:23:15.107 に答える