I have a instance and I am new to C# delegates , Func , Expression and action . I want to pass a parameter as an expression which would check for few conditions and then it would return or perform based on some conditions provided something like this
var result = Uow.GroupLicense.Set
.Join(Uow.UserGroup.Set, x => x.GroupGuid, y => y.GroupGuid, (GL, G) => new { G, GL })
.Join(Uow.Users.Set, x => x.G.UserGuid, y => y.UserGuid, (UG, U) => new { UG, U })
.SingleOrDefault(x => x.U.UserID == Username);
if (result != null && result.UG.GL.IsActive && result.U.IsEnabled && !result.U.IsLocked)
{
SessionStorage.LoginAttempts = UpdateLoginAttempts(Username, true);
SessionStorage.SessionID = HttpContext.Current.Session.SessionID;
SessionStorage.LoginAttempts = 0;
SessionStorage.UserName = Username;
SessionStorage.ABBUserName = Username;
}
else if (!result.UG.GL.IsActive)
{
result.U.IsEnabled = false;
result.U.IsLocked = true;
Uow.Users.Update(result.U);
Uow.Commit();
ErrorMessage = "User License Expired";
}
else if (result.U.IsLocked)
{
ErrorMessage = "User is locked";
}
else if (!result.U.IsEnabled)
{
ErrorMessage = "User is disabled by administrator";
}
else
{
ErrorMessage = "User doesnt exist ";
}
Now this is my original code . I want to convert this into a decision tree based on two class item conditions . Something like this
if this -> Boolean result = EnsureLicense((x, y) => x.IsEnabled && y.IsActive);
then do Action1
if This -> Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsActive);
then do Action2
if This - > Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsLocked);
then do Action3.
I dont wanna switch it into something like If else structure code with multiple if else and else if structure code . How to proceed with this kind of strategy since I cannot put into switch case .
I want to do it with Actions and Expression and Func delegates . I want to learn how to proceed with this kind of strategy .
Please guide me