Linq In Clause & Predicate building

Posted by Michael G on Stack Overflow See other posts from Stack Overflow or by Michael G
Published on 2010-05-18T13:16:48Z Indexed on 2010/05/18 13:20 UTC
Read the original article Hit count: 277

Filed under:
|
|

I have two tables. Report and ReportData. ReportData has a constraint ReportID.

How can I write my linq query to return all Report objects where the predicate conditions are met for ReportData? Something like this in SQL:

SELECT * FROM Report as r
Where r.ServiceID = 3 and r.ReportID IN (Select ReportID FROM ReportData WHERE JobID LIKE 'Something%')

This is how I'm building my predicate:

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

This is my query construction at the moment:

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql