使用Linq to Entities在winforms应用程序中填充组合框,一个组合框依赖于另一个组合框

本文关键字:组合 一个 另一个 依赖于 Linq winforms Entities 应用程序 使用 填充 to | 更新日期: 2023-09-27 18:08:58

我的代码;

        comboBoxAdminVisit.DataSource = be.Events;
        comboBoxAdminVisit.DisplayMember = "EventName";

        var fff = (from cc in be.Visitors
                  select cc.Attending).FirstOrDefault();
        var ggg = (from xx in be.Events
                   where xx.Id == fff
                   select xx.Id).FirstOrDefault();
        if (fff == ggg)
        {
            foreach (var name in comboBoxAdminName.Items)
            {
                comboBoxAdminName.Items.Add(name);
            }

cc。attend是一个外键,它存储事件表的主键。我想从第一个组合框中选择一个事件,并在第二个组合框中填充参加该事件的人员。提前感谢!

Events       Visitors
------       --------
Id           Id
Attendee(FK) Name
EventName    Company
EventStart   Car Reg
EventEnd     Visiting

使用Linq to Entities在winforms应用程序中填充组合框,一个组合框依赖于另一个组合框

我不知道你的实体的确切结构,但这里有一个如何实现它的想法:

private void Form1_Load(object sender, EventArgs e)
{
    comboBoxAdminVisit.DataSource = be.Events;
    comboBoxAdminVisit.DisplayMember = "EventName";
}
private void comboBoxAdminVisit_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBoxAdminVisit.SelectedItem != null)
    {
        Event selectedEvent = (Event)comboBoxAdminVisit.SelectedItem;
        var visitors = (from cc in be.Visitors
                        where cc.Attending.Events.Contains(x => x.EnventId = selectedEvent.Id)
                        select cc);
        comboBoxAdminName.DataSource = visitors;
        comboBoxAdminName.DisplayMember = "Name";
    }
}