# Call method by condition [closed]

I apologize in advance for my English.

I have a user search method. It's parameters - login and strict. The last one determines, which method should have been used for search - Equals (strict is true) or Contains (strict is false). How should I implement it? I could do two different methods, but I think it would be code repeating.

    public async Task<List<UserFindInfoDTO>> FindUserByLoginAsync(string login, bool strict)
{
var users = await context.Users
.Where(u => u.Nickname.Equals(login)) // change on "Contains" if strict is false
.Select(u => new UserFindInfoDTO
{
Status  = u.Status,
Id = u.Id,
MiniAvatar =  u.Image,
Nickname = u.Nickname
})
.ToListAsync();
...
return users;
}

• If the code is unfinished, it's not ready for review. Please take a look at the help center. – Mast Jul 3 '20 at 6:06

The lambda expression you pass to Where() is where you define the condition, so this is where it makes the most sense to put all the logic for this.

.Where(u => strict ? u.Nickname.Equals(login) : u.Nickname.Contains(login))


Or for higher performance you can conditionally pass a different lambda expression:

Func<User,bool> checker = strict

• @ZOOMSMASH Also I just realized I assigned the type bool to the lambda input instead of the user object! – potato Jul 2 '20 at 22:07