C
C#3y ago
Alerin

Passing the where parameters by the function

How do I get this sample code to work? Unfortunately, it does not want to work.
public async Task<User> GetAsync(Func<Models.User, bool> selector)
{
_model = await this.GetData().Where(x => selector(x)).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
public async Task<User> GetAsync(Func<Models.User, bool> selector)
{
_model = await this.GetData().Where(x => selector(x)).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
Use:
var user = await User.GetAsync(x => x.Mail == Model.Identifier || (x.Phone == Model.Identifier && x.PhoneCode == Model.PhoneCode));
var user = await User.GetAsync(x => x.Mail == Model.Identifier || (x.Phone == Model.Identifier && x.PhoneCode == Model.PhoneCode));
5 Replies
Angius
Angius3y ago
Try just .Where(selector) Ah, is this EF?
Alerin
AlerinOP3y ago
Yes
Angius
Angius3y ago
If so, you might need Expression<Func<User, bool>> instead At least Select()s need that Expression<> wrapper
Alerin
AlerinOP3y ago
public async Task<User> GetAsync(Expression<Func<Models.User, bool>> predicate)
{
_model = await this.GetData().Where(predicate).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
public async Task<User> GetAsync(Expression<Func<Models.User, bool>> predicate)
{
_model = await this.GetData().Where(predicate).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
It actually works. Thanks!
Angius
Angius3y ago
Nice
Want results from more Discord servers?
Add your server