P
Paolo
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.Value;
qryEnd = dtmpickEnd.Value;
var qryTrans =
from trans in dataSet.Transaction // 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_Category,
trans.T_SubCategory,
trans.T_PayMethod,
trans.T_Amount
};
// if Payee and Category selected, get transactions for both
if ((chkbxPayee.Checked) && (chkbxCategory.Checked))
{
if (!string.IsNullOrEmpty(qryPayeeId))
qryTrans =
qryTrans.Where(trans => trans.T_PayeeId ==
qryPayeeId);
if (!string.IsNullOrEmpty(qryCatId))
qryTrans =
qryTrans.Where(trans => trans.T_Category == qryCatId);
}
// if Category selected, get transactions for the Category only
if (chkbxCategory.Checked)
{
if (!string.IsNullOrEmpty(qryCatId))
qryTrans =
qryTrans.Where(trans => trans.T_Category ==
qryCatId);
}
// if Payee selected, get transactions for the Payee only
if (chkbxPayee.Checked)
{
if (!string.IsNullOrEmpty(qryPayeeId))
qryTrans =
qryTrans.Where(trans => trans.T_PayeeId ==
qryPayeeId);
}
dgvSearchResults.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.Value;
qryEnd = dtmpickEnd.Value;
var qryTrans =
from trans in dataSet.Transaction // 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_Category,
trans.T_SubCategory,
trans.T_PayMethod,
trans.T_Amount
};
// if Payee and Category selected, get transactions for both
if ((chkbxPayee.Checked) && (chkbxCategory.Checked))
{
if (!string.IsNullOrEmpty(qryPayeeId))
qryTrans =
qryTrans.Where(trans => trans.T_PayeeId ==
qryPayeeId);
if (!string.IsNullOrEmpty(qryCatId))
qryTrans =
qryTrans.Where(trans => trans.T_Category == qryCatId);
}
// if Category selected, get transactions for the Category only
if (chkbxCategory.Checked)
{
if (!string.IsNullOrEmpty(qryCatId))
qryTrans =
qryTrans.Where(trans => trans.T_Category ==
qryCatId);
}
// if Payee selected, get transactions for the Payee only
if (chkbxPayee.Checked)
{
if (!string.IsNullOrEmpty(qryPayeeId))
qryTrans =
qryTrans.Where(trans => trans.T_PayeeId ==
qryPayeeId);
}
dgvSearchResults.DataSource = qryTrans.ToList();
}