我有一个具有下一格式的用户类
List<Subscriptions> Subscriptions {get;set;}
类订阅包含:
SubscriptionType type {get;set;}
我想将所有这些包含在User对象中,如下所示:
var _referredUser = ctx.Users
.Include(x=>x.Subscriptions.Where(y=>y.Status==true))
.ToList()
.FirstOrDefault(y => y.Email == _all[i].Referred_email);
我可以成功地包含订阅集合,但我不确定如何进一步超越Subscription集合,并将属性SubscriptionType包含在User对象中,如果可能的话?
我的另一个问题是,我是否只能包含那些状态为== true的订阅,因为用户可以在订阅表中有多个记录而只有一个设置为true?
我试过这样的事情,但它给我一个错误:
.Include(x=>x.Subscriptions.Where(y=>y.Status==true))
错误是:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
这样的事情应该有用......
var email = _all[i].Referred_email;
var _referredUser = ctx.Users
.Include(x=>x.Subscriptions.Select(y=>y.SubscriptionType))
.Select(x => new { User = x, Subscriptions = x.Subscriptions.Where(subscription => subscription.Status)})
.Where(x => x.Subscriptions.Any())
.Select(x => x.User)
.FirstOrDefault(y => y.Email == email);
要包含子属性,可以在include语句中使用Select
。
要仅获得Subscriptions.Status为true的结果,您需要执行单独的查询或执行一些Select()魔术。
答案 1 :(得分:2)
您无法在Include
中进行过滤,我建议您这样做:
var email=_all[i].Referred_email;
var _referredUser = ctx.Users.Include(x=>x.Subscriptions.Select(y=>y.SubscriptionType))
.FirstOrDefault(y => y.Email == email && y.Subscriptions.Any(y=>y.Status));
如异常所述,您必须在Include