我正在使用带有实体框架工作的web api 2。在一个条件下,我想知道如何减少if/else
相同的代码。我努力了,但我没有得到任何结果。我正在使用lambada表达。
// 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);
}
}
答案 0 :(得分:1)
看起来你只是在检查每个参数是否存在,如果它们不是null(或0),你可以通过它进行过滤。您可以检查每个参数并链接Where
次来完成您想要的操作:
var query = db.tblEvents;
if(FightTypeId != 0)
{
query = query.Where(e => e.FightTypeId == FightTypeId);
}
if(Title != null)
{
query = query.Where(e => e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()));
}
if(Date != null)
{
query = query.Where(e => e.Date == Date);
}
var res = query.Select(e =>
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);
答案 1 :(得分:0)
你可以这样简化它:
// by defaultly shows all Details to user
var getTblEvent = db.tblEvents;
// ALL Value Is Given By User
if (FightTypeId != 0 && Title != null && Date != null)
{
getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId && e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()) && e.Date == Date);
}
//Only FightTypeID Is Given By User
else if (FightTypeId != 0 && Title == null && Date == null)
{
getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId);
}
//TITLE Is Given By User
else if (FightTypeId == 0 && Title != null && Date == null)
{
getTblEvent = db.tblEvents.Where(e => e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()));
}
// Only DATE Is Given By User
else if (FightTypeId == 0 && Title == null && Date != null)
{
getTblEvent = db.tblEvents.Where(e => e.Date == Date);
}
//FIGHTTYPEID And TITLE Is Given By User
else if (FightTypeId != 0 && Title != null && Date == null)
{
getTblEvent = db.tblEvents.Where(e => e.FightTypeId == FightTypeId && e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()));
}
// Only DATE And TITLE Is Given By User
else if (FightTypeId == 0 && Title != null && Date != null)
{
getTblEvent = db.tblEvents.Where(e=>e.Title.ToUpper().Trim().Contains(Title.ToUpper().Trim()) && e.Date==Date);
}
//FightTypeID And Date Is Given By User
else if (FightTypeId != 0 && Title == null && Date != null)
{
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);