简体   繁体   English

EF Core 查询多对多过滤

[英]EF Core Query Many to Many with filtering

I have the following table structure:我有以下表结构:

在此处输入图片说明

I want to retrieve all funds for provided reportId .我想检索提供的reportId 的所有资金。 I did it this way:我是这样做的:

                var result = _context.FundsInReports
                        .Join(_context.Funds,
                                a=> a.FundId,
                                b => b.Id,
                                (fir, fund) => new {fir, fund})
                        .Join(_context.Reports,
                                a=> a.fir.ReportId,
                                b=> b.Id,
                                (fir2, report) => new { fir2, report})
                        .Where(q=> q.fir2.fir.ReportId==reportId)
                        .Select(res => new FundsResponse()
                        {
                            FundId = res.fir2.fund.Id,
                            LegalName = res.fir2.fund.LegalName,
                            HeaderName = res.fir2.fund.HeaderName,
                            PortfolioCurrency = res.fir2.fund.PortfolioCurrencyId,
                            BaseCurrency = res.fir2.fund.BaseCurrencyId,
                            FileName = res.fir2.fund.FileName,
                            Locked = res.fir2.fund.Locked

                        }).ToList();

and this works fine... However, I would like to use this code:这工作正常......但是,我想使用以下代码:

                var result = _context.Funds
                    .Include(a => a.FundsInReports)
                    .ThenInclude(a => a.Report)         // Many to many , intellisense is not working here !
                    .Select(res => new FundsResponse()
                    {
                        FundId = res.Id,
                        LegalName = res.LegalName,
                        HeaderName = res.HeaderName,
                        PortfolioCurrency = res.PortfolioCurrencyId,
                        BaseCurrency = res.BaseCurrencyId,
                        FileName = res.FileName,
                        Locked = res.Locked

                    }).ToList();

but I don't know how to add filtering (where) into this code.但我不知道如何在此代码中添加过滤(在哪里)。 Thanks...谢谢...

You don't use Include with custom projections.您不将 Include 与自定义投影一起使用。 They are alternative approaches to generating results.它们是产生结果的替代方法。 So just run所以就跑

var result = _context.FundsInReport
    .Where( fr => fr.ReportId == someId )
    .Select(fr => new FundsResponse()
    {
        FundId = fr.Fund.Id,
        LegalName = fr.Fund.LegalName,
        HeaderName = fr.Fund.HeaderName,
        PortfolioCurrency = fr.Fund.PortfolioCurrencyId,
        BaseCurrency = fr.Fund.BaseCurrencyId,
        FileName = fr.Fund.FileName,
        Locked = fr.Fund.Locked
    }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM