【问题标题】:.NET Core EF Select object where relation has.NET Core EF 选择关系所在的对象
【发布时间】:2018-09-26 09:25:57
【问题描述】:

我试图弄清楚如何从模型具有具有特定属性的关系模型的 DBSet 中获取模型。

例子:

List<Person> persons = context.Person.Where(p => p.Properties.Where(p => p.CountryCode == 1)).ToList();

一个人可以有多个属性,我只想获取在特定国家拥有属性的人的列表。

【问题讨论】:

  • 如果您至少提供上下文类和数据库表结构,那么回答正确会简单得多......如果您基本上有 2 个表,那么加入就足够了,否则需要其他解决方案

标签: .net entity-framework .net-core


【解决方案1】:

其实你可以试试这样的,

_unitOfWork.GetRepository<AdvertTrade>()
                .GetQueryable()
                .AsNoTracking()
                // Make sure the advert trade does not have any related to this user
                .Include(at => at.UserRatings)
                .Where(at => at.Id.Equals(userRating.AdvertTradeId))
                .Any(at => at.UserRatings.Any(ur => ur.CreatedBy.Value.Equals(userId) && ur.DeletedOnUtc == null))

我没有发送垃圾邮件 .Where(),而是使用 Any() 来查找嵌套需求。

为了让事情更清楚,这里有一个示例代码块,旧代码被注释掉了,你需要关注注释的代码(我没有重构它,因为它不好,但是因为我的 ERD 已更改)。

var payload = _unitOfWork.GetRepository<CurrencySource>()
                .GetQueryable()
                .AsNoTracking()
                // Make sure all currency sources are not disabled or deleted
                .Where(cs => cs.IsEnabled && cs.DeletedOnUtc == null)
                //.Include(x=>x.CurrencyPairComponents)
                //.ThenInclude(x=>x.CurrencyPair)
                //.ThenInclude(x=>x.PartialCurrencyPairs)
                //.Include(cs => cs.CurrencyPairs)
                //.ThenInclude(cp => cp.PartialCurrencyPairs)
                //.ThenInclude(pcp => pcp.Currency)
                //.Where(cs => cs.CurrencyPairComponents.Select(x=>x.CurrencyPair)
                //    // Make sure all currencypairs are not disabled or deleted
                //    .Any(cp => cp.IsEnabled && cp.DeletedOnUtc == null
                //               &&
                //               // Make sure none of the currency pair's partial currency pair is not disabled or deleted
                //               cp.PartialCurrencyPairs
                //                   .Any(pcp => pcp.Currency.IsEnabled && pcp.Currency.DeletedOnUtc == null)))
                .Select(cs => new
                {
                    id = cs.Id,
                    abbreviation = cs.Abbreviation,
                    name = cs.Name
                    //currencyPairs = cs.CurrencyPairComponents
                    //    .Select(cp => new
                    //    {
                    //        id = cp.Id,
                    //        partialCurrencyPairs = cp.CurrencyPair.PartialCurrencyPairs
                    //            .Select(pcp => new
                    //            {
                    //                currencyId = pcp.CurrencyId,
                    //                currency = new
                    //                {
                    //                    abbrv = pcp.Currency.Abbrv,
                    //                    currencyTypeId = pcp.Currency.CurrencyTypeId,
                    //                    currencyType = new
                    //                    {
                    //                        typeShortForm = pcp.Currency.CurrencyTypeId.GetDisplayName(),
                    //                        name = pcp.Currency.CurrencyTypeId.GetDisplayName()
                    //                    },
                    //                    name = pcp.Currency.Name,
                    //                    walletTypeId = pcp.Currency.WalletTypeId
                    //                },
                    //                isMain = pcp.IsMain
                    //            })
                    //    })
                });

请注意,我嵌套了一些 LINQ 声明。

.Where(cs => cs.CurrencyPairComponents.Select(x=>x.CurrencyPair)
                    // Make sure all currencypairs are not disabled or deleted
                    .Any(cp => cp.IsEnabled && cp.DeletedOnUtc == null
                               &&
                               // Make sure none of the currency pair's partial currency pair is not disabled or deleted
                               cp.PartialCurrencyPairs
                                   .Any(pcp => pcp.Currency.IsEnabled && pcp.Currency.DeletedOnUtc == null)))

【讨论】:

    【解决方案2】:

    您显然是在尝试加入。 我想你想要(伪代码)

    var persons = (from p in Context.Person
                  join pr in Context.Property on p.ID equals pr.PersonID
                  where pr.CountryCode == 1
                   select p).ToList();
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2019-03-06
      • 2021-09-15
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      相关资源
      最近更新 更多