开发者

Using "from in" in a LinqSpecs specification?

开发者 https://www.devze.com 2023-02-05 02:06 出处:网络
We\'re using LinqSpecs to create specifications for our NHibernate Linq queries and I havethe following query:

We're using LinqSpecs to create specifications for our NHibernate Linq queries and I have the following query:

from p in projects
  from pp in p.Personprojects
  where pp.Id.PersonId == userId
  select p

And I'd like to encapsulate the

 from pp in p.Personprojects   
 where pp.Id.PersonId == userId

part in a specification.

Is there any way to do that?

My current solution is

public override Expression<Func<Project, bool>> IsSatisfie开发者_StackOverflow社区dBy()
{
  return p => p.Personprojects.Count(pp => pp.Id.PersonId == _userId)>0;
}

which doesn't strike me as optimal...


Instead of comparing Count to 0, it's better to use Any:

public override Expression<Func<Project, bool>> IsSatisfiedBy()
{
    return p => p.Personprojects.Any(pp => pp.Id.PersonId == _userId);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号