Linq In 子句 &谓词构建

发布于 2024-09-02 07:06:22 字数 1786 浏览 6 评论 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;
    }

我也尝试执行以下操作: 公共覆盖 IQueryable GetReports(Expression> predicate, LLReportsDataContext reportDC) { if (reportDC == null) 抛出 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));

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

I've tried doing the following as well:
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;
    }

However, I get the this Exception: "Unsupported overload used for query operator 'Where'."

UPDATE
This fixed it:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-09-09 07:06:22

查询

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

关于LinqKit的

在使用LinqKit时,有时需要在实体集合中调用AsExpandable()并编译谓词表达式。请参阅此示例:): 如何-使用-predicate-builder-with-linq2sql-and-or-operator

Query

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

AboutLinqKit

When using LinqKit, sometimes you need to call AsExpandable() in the entity collection and to compile the predicate expression. see this example : ): how-to-use-predicate-builder-with-linq2sql-and-or-operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文