如何将这两种方法合并为一种?
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get { return a => a.Activity.Parent.ActivityUsers.Any(x => (x.User.Id == id)); }
}
和
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get { return a => a.Activity.ActivityUsers.Any(x => (x.User.Id == id)); }
}
值得注意的是,我的想法是我想查看根记录以获取某个用户的链接。但是,a
可能是一个根或可能是一个孩子(仅一个后代/级别;不是递归的)。我想要一个ORing,可以这么说,这两个Linq表达式。
答案 0 :(得分:1)
可能是这样的:
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get
{
return a => a.Activity.Parent.ActivityUsers
.Concat(a.Activity.ActivityUsers).Any(x => (x.User.Id == id));
}
}
或者因为你只担心一个级别,你可以使用:
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get
{
return a => a.Activity.Parent.Any(a2 =>
a2.ActivityUsers.Any(x =>
(x.User.Id == id) || x.Activity.ActivityUsers.Any(y =>
y.User.Id == id)));
}
}
这个选项可能更合适。
答案 1 :(得分:0)
public override Expression<Func<Attachment, bool>> MatchingCriteria
{
get { return a => a.Activity.Parent.ActivityUsers.Any(x => (x.User.Id == id))
|| a.Activity.ActivityUsers.Any(x => (x.User.Id == id));
}
}