Is there any way I can avoid the ugly and repetitive 'if' construct shown
below?
I can't refactor into a separate method because qryTrans is an anonymous type.
I don't think I can use a switch statement either.
It works fine but is not aesthetically pleasing!
private void btnSearch_Click (object sender, EventArgs e)
{
qryStart = dtmpickStart.Va lue;
qryEnd = dtmpickEnd.Valu e;
var qryTrans =
from trans in dataSet.Transac tion // get data for all
transactions
where ((trans.T_Date >= qryStart) // between start and end
dates
&& (trans.T_Date <= qryEnd))
select new
{
trans.T_Date,
trans.T_PayeeId ,
trans.T_Categor y,
trans.T_SubCate gory,
trans.T_PayMeth od,
trans.T_Amount
};
// if Payee and Category selected, get transactions for both
if ((chkbxPayee.Ch ecked) && (chkbxCategory. Checked))
{
if (!string.IsNull OrEmpty(qryPaye eId))
qryTrans =
qryTrans.Where( trans =trans.T_PayeeI d ==
qryPayeeId);
if (!string.IsNull OrEmpty(qryCatI d))
qryTrans =
qryTrans.Where( trans =trans.T_Catego ry == qryCatId);
}
// if Category selected, get transactions for the Category only
if (chkbxCategory. Checked)
{
if (!string.IsNull OrEmpty(qryCatI d))
qryTrans =
qryTrans.Where( trans =trans.T_Catego ry ==
qryCatId);
}
// if Payee selected, get transactions for the Payee only
if (chkbxPayee.Che cked)
{
if (!string.IsNull OrEmpty(qryPaye eId))
qryTrans =
qryTrans.Where( trans =trans.T_PayeeI d ==
qryPayeeId);
}
dgvSearchResult s.DataSource = qryTrans.ToList ();
}
below?
I can't refactor into a separate method because qryTrans is an anonymous type.
I don't think I can use a switch statement either.
It works fine but is not aesthetically pleasing!
private void btnSearch_Click (object sender, EventArgs e)
{
qryStart = dtmpickStart.Va lue;
qryEnd = dtmpickEnd.Valu e;
var qryTrans =
from trans in dataSet.Transac tion // get data for all
transactions
where ((trans.T_Date >= qryStart) // between start and end
dates
&& (trans.T_Date <= qryEnd))
select new
{
trans.T_Date,
trans.T_PayeeId ,
trans.T_Categor y,
trans.T_SubCate gory,
trans.T_PayMeth od,
trans.T_Amount
};
// if Payee and Category selected, get transactions for both
if ((chkbxPayee.Ch ecked) && (chkbxCategory. Checked))
{
if (!string.IsNull OrEmpty(qryPaye eId))
qryTrans =
qryTrans.Where( trans =trans.T_PayeeI d ==
qryPayeeId);
if (!string.IsNull OrEmpty(qryCatI d))
qryTrans =
qryTrans.Where( trans =trans.T_Catego ry == qryCatId);
}
// if Category selected, get transactions for the Category only
if (chkbxCategory. Checked)
{
if (!string.IsNull OrEmpty(qryCatI d))
qryTrans =
qryTrans.Where( trans =trans.T_Catego ry ==
qryCatId);
}
// if Payee selected, get transactions for the Payee only
if (chkbxPayee.Che cked)
{
if (!string.IsNull OrEmpty(qryPaye eId))
qryTrans =
qryTrans.Where( trans =trans.T_PayeeI d ==
qryPayeeId);
}
dgvSearchResult s.DataSource = qryTrans.ToList ();
}
Comment