So in a function I've a large queryable and I apply a bunch of where cause on it based on other conditions.
Like in this example:
query.Where(i =>
_context.FicDernierEvt
.Where(y => y.VteAffaire == null && y.ApvAffaire == null)
.Select(y => y.IdFicheCrm)
.Contains(i.Id)
);
I've this condition _context.FicDernierEvt.Where(y => y.VteAffaire == null && y.ApvAffaire == null).Select(y => y.IdFicheCrm).Contains(i.Id)
that is used a lot in my code.
I would like to avoid having this all accross my code so i've tried to make a function:
private bool isProspect(FicFicheCrm ficheCrm){
return _context.FicDernierEvt
.Where(y => y.VteAffaire == null && y.ApvAffaire == null)
.Select(y => y.IdFicheCrm)
.Contains(ficheCrm.Id);
}
So i could use it this way:
query.Where(i => isProspect(i));
But it didn't worked since, it's just not mean to be done that way.
Do someone have an idea on how to make reusable conditions like this to be used in queryables ?
CodePudding user response:
you can use LinqKit, it's a free tool that easy help in this situation.
Or you can create your function, see this question
CodePudding user response:
I suspect it didn't work because your _context
was inside a using
statement and you weren't referencing it properly. You could make your helper method pure, by explicitly passing in the _context, and then it should behave identically as your first code block.
private bool isProspect(MyContextType _context, FicFicheCrm ficheCrm) {
and
query.Where(i => isProspect(_context, i));