如何解决C#创建一个获取特定银行所有分支的数据源
|| 我正在尝试为绑定导航器创建一个绑定源,该绑定源将能够显示特定银行中的所有分支机构。 获取数据源的语句如下branchMasterBindingSource.DataSource = Program.Kernel.Get<IBranchMasterService>().GetAllBranchMasters();
但是,这将拉动所有分支机构,无论它们属于哪个银行。
我需要知道如何更改它,以使它获得AllBranchMasters,其中数据库中的字段(\“ U_bank_code \”)等于名为\“ cb_bank_code \”的组合框
额外的代码如下:
private void cb_bank_code_SelectedindexChanged(object sender,EventArgs e)
{
branchMasterBindingSource.DataSource = null;
branchMasterBindingSource.DataSource = Program.Kernel.Get<IBranchMasterService>().GetAllBranchMasters();
//clear textfields after input
lbl_show_bank_name.Text = string.Empty;
txt_branch_code.Text = string.Empty;
txt_branch_name.Text = string.Empty;
txt_swift_sort_code.Text = string.Empty;
txt_address_1.Text = string.Empty;
txt_address_2.Text = string.Empty;
txt_comments.Text = string.Empty;
var bankMasterService = Program.Kernel.Get<IBankMasterService>();
var bankMasters = from bm in bankMasterService.GetAllBankMasters()
where bm.U_Bank_code.Trim().Equals(cb_bank_code.Text.Trim(),StringComparison.CurrentCultureIgnoreCase)
select bm;
if (bankMasters.Any(x => x != null))
{
var bankMaster = bankMasters.First();
lbl_show_bank_name.Text = bankMaster.U_Bank_name;
CbBankCode = bankMaster.U_Bank_code;
}
else
{
//clear textfields after input
lbl_show_bank_name.Text = string.Empty;
}
我是C#和点网的新手,不知道如何更改语句的语法。任何帮助表示赞赏
解决方法
尽管您应该考虑重构数据访问方法以进行过滤,但类似的事情应该可以为您解决问题
private void cb_bank_code_SelectedIndexChanged(object sender,EventArgs e)
{
ClearTextfieldsAfterInput();
branchMasterBindingSource.DataSource = GetSelectedBranchMasters();
var bankMasters = GetSelectedBankMaster();
if (bankMasters.Any(x => x != null))
{
var bankMaster = bankMasters.First();
lbl_show_bank_name.Text = bankMaster.U_Bank_name;
CbBankCode = bankMaster.U_Bank_code;
}
else
{
//clear textfields after input
lbl_show_bank_name.Text = string.Empty;
}
}
private IEnumerable<BankMaster> GetSelectedBankMaster()
{
var selectedBank = cb_bank_code.Text.Trim();
return Program.Kernel.Get<IBankMasterService>()
.GetAllBankMasters()
.Where(bm => bm.U_Bank_code.Trim().Equals(selectedBank,StringComparison.CurrentCultureIgnoreCase))
.ToList();
}
private IEnumerable<BranchMaster> GetSelectedBranchMasters()
{
var selectedBank = cb_bank_code.Text.Trim();
return Program.Kernel.Get<IBranchMasterService>()
.GetAllBranchMasters()
.Where(branch => string.Equals(branch.U_bank_code,selectedBank,StringComparison.CurrentCultureIgnoreCase))
.ToList();
}
private void ClearTextfieldsAfterInput()
{
lbl_show_bank_name.Text = \"\";
txt_branch_code.Text = \"\";
txt_branch_name.Text = \"\";
txt_swift_sort_code.Text = \"\";
txt_address_1.Text = \"\";
txt_address_2.Text = \"\";
txt_comments.Text = \"\";
}
, 如果我对您的理解正确,并且对这些类型进行了一些假设,则应该可以进行以下操作:
branchMasterBindingSource.DataSource = Program.Kernel.Get<IBranchMasterService>().GetAllBranchMasters.Where(x => x.U_Bank_code.Trim().Equals(cb_bank_code.Text.Trim(),StringComparison.CurrentCultureIgnoreCase))
我猜
Program.Kernel.Get<IBranchMasterService>().GetAllBranchMasters
返回IEnumerable或List。对于返回所有库/分支的函数,您将无能为力,除非您执行以下任一操作:
更改GetAllBranchMasters以返回IQueryable-然后可以像上面一样应用过滤器,并在末尾添加.ToList()以执行查询。
(推荐)创建第二种方法,该方法接受银行代码,并根据基础DAL返回分支。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。