我试图弄清楚如何从DBSet获得模型,其中模型具有具有特定属性的关系模型。
示例:
List<Person> persons = context.Person.Where(p => p.Properties.Where(p => p.CountryCode == 1)).ToList();
一个人可以拥有多个财产,我只想获取在特定国家/地区拥有财产的人的列表。
答案 0 :(得分:0)
您显然正在尝试加入。 我想你要(伪代码)
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();
答案 1 :(得分:0)
您实际上可以尝试类似的事情,
_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)))