我有一個方法,有四個參數,我用來過濾我的數據,有時4個參數填充,有時只有三個或兩個或一個被填充,所以我正在尋找一種方法來採取只有必要的參數值。例如,如果用戶只輸入一個像startDate這樣的參數,我想用該日期恢復所有數據,而不使用其他參數,如null
,但不應考慮這些參數,我的方法將搜索具有正確StartDate的數據和其他參數null
我不想這樣做方法與可選參數
internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, decimal? Type)
{
using (Model m = new Model())
{
return m.DBContext
.InconsistencyDebtors
.Join(m.DBContext.Inconsistencies,
u => u.InconsistencyId,
uir => uir.InconsistencyId,
(u, uir) => new { u, uir })
.Join(m.DBContext.InconsistencyDebtorDocuments,
r => r.uir.InconsistencyId,
ro => ro.InconsistencyId,
(r, ro) => new { r, ro })
.Where(g =>
g.r.uir.InconsistencyStateId == State &&
g.r.uir.InconsistencyTypeId == Type &&
g.r.uir.InsDate >= StartDate &&
EnDate >= g.r.uir.InsDate)
.Select(g => new Inconsistence()
{
ParticipantCode = g.r.u.ParticipantCode,
DebtorId = g.ro.DebtorId,
InconsistencyTypeId = g.r.uir.InconsistencyTypeId,
InconsistencyStateId = g.r.uir.InconsistencyStateId,
DateInconsistence = g.r.uir.InsDate
})
.ToList();
}
}
我建議你要學習一些關於重載,http://csharpindepth.com/Articles/General/Overloading.aspx。 –
你的問題不清楚。你不想做什麼? –