I am using web api 2 with entity frame work. On one condition I would like to know how I can reduce if/else for the same code. I tried so hard but I do not get any result. I am using lambada expression.
// ALL Value Is Given By User
if (FightTypeId != 0 && Title != null && Date != null)
{
var getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId && e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()) && e.Date == Date);
//this illustrates that user can give title in any case and title can be in 2-3 latter of title is enough for get output
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
//Only FightTypeID Is Given By User
else if (FightTypeId != 0 && Title == null && Date == null)
{
var getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId);
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
//TITLE Is Given By User
else if (FightTypeId == 0 && Title != null && Date == null)
{
var getTblEvent = db.tblEvents.Where(e => e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()));
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
// Only DATE Is Given By User
else if (FightTypeId == 0 && Title == null && Date != null)
{
var getTblEvent = db.tblEvents.Where(e => e.Date == Date);
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
//FIGHTTYPEID And TITLE Is Given By User
else if (FightTypeId != 0 && Title != null && Date == null)
{
var getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId && e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()));
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
// Only DATE And TITLE Is Given By User
else if (FightTypeId == 0 && Title != null && Date != null)
{
var getTblEvent = db.tblEvents.Where(e=>e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()) && e.Date==Date);
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
//FightTypeID And Date Is Given By User
else if (FightTypeId != 0 && Title == null && Date != null)
{
var getTblEvent = db.tblEvents.Where(e => e.FightTypeId==FightTypeId && e.Date==Date);
var res = from e in getTblEvent
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
// by defaultly shows all Details to user
else
{
var res = from e in db.tblEvents
select new
{
EventID = e.EventID,
FightTypeId = e.FightTypeId,
Title = e.Title,
Date = e.Date,
Location = e.Location,
UserSelectFavoriteFlag = e.UserSelectFavoriteFlag,
Price = e.Price,
UserPredictionFlag = e.UserPredictionFlag,
PredictionStartDate = e.PredictionStartDate,
PredictionEndDate = e.PredictionEndDate,
ModifiedUserId = e.ModifiedUserId,
LastUserModifiedDate = e.LastUserModified,
};
return Ok(res);
}
}
You could streamline it like this: