linq查询和foreign用于创建数据

本文关键字:创建 数据 用于 foreign 查询 linq | 更新日期: 2024-05-15 22:56:26

我遇到了一个问题,即我正在使用表"locationstation"并在内部创建站点和位置,并且站点和位置都通过主键链接到locationstation。我已经成功地在组合框中显示了他们的数据,但现在的问题是我不知道如何选择组合框中的数据并将数据保存在locationstation表中。

 private void btnCreate_Click(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            //station selectStation = cbStation.SelectedItem as station;
            //location selectLocation = cbLocation.SelectedItem as location;
            string selectStation = cbStation.SelectedItem.ToString();
            string selectLocation = cbLocation.SelectedItem.ToString();
            locationstation creLS = new locationstation();
            creLS.idStation = cbStation.SelectedItem.ToString();
            selectLocation.Location1 = (string)cbLocation.SelectedItem;
            Setupctx.locationstations.AddObject(selectStation);
            //Setupctx.SaveChanges();
            //cbStation.SelectedIndex = -1;
            //cbLocation.SelectedIndex = -1;
            MessageBox.Show("New Location Station Is Created");
        }
    }

我不知道如何让它工作,但我正在尝试的代码就在这里。我们将非常感谢您的帮助。

这是我将站点名称和位置名称绑定到组合框中的代码。

private void Create_LS_Load(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            var storeStation = (from SLS in Setupctx.locationstations
                                        join station s in Setupctx.stations on SLS.idStation equals s.idstations
                               select s.Station1).Distinct().ToList();                                   
            foreach (var LocationStation in storeStation)
            {
                cbStation.Items.Add(LocationStation);
            }
            var storeLocation = (from SLS in Setupctx.locationstations
                                join location l in Setupctx.locations on SLS.idLocation equals l.idlocation
                                select l.Location1).Distinct().ToList();                                      
            foreach (var LocationStation1 in storeLocation)
            {
                cbLocation.Items.Add(LocationStation1);
            }
        }
    }

linq查询和foreign用于创建数据

Hi在使用添加对象之前,将selectStation的所有导航属性设置为null

Setupctx.locationstations.AddObject(selectStation);

而不是

string selectStation=cbStation.SelectedItem.ToString();

使用

LocationStation selectStation=(LocationStation)cbStation.SelectedItem;

然后从selectStation中提取值或使用它执行您想要的操作。我希望这会有所帮助。

Station selectStation = (Station)cbStation.SelectedItem ; //cast here to your T
Location selectLocation = (Location)cbLocation.SelectedItem; //cast here to your T
locationstation creLS = new locationstation() 
{
 StationId=selectStation.Id ,
 LocationId=selectLocation.Id
};
Setupctx.locationstations.AddObject(creLS);
Setupctx.SaveChanges();

然而,我无法想象在具有Key/Value数据的组合框上执行类似于上面的操作。因为可能通过使用.ToDictionary()将查找类型组合框绑定到KeyValuePair(其中T是主键的类型)打开了更容易处理的代码重用通过设置ValueMember=Key,在整个应用程序中的任何组合框上选择项目和绑定和DisplayMember=Value(或组合控件中的任何Key/Value属性),则可以这样做,例如:

long GetSelectedId(comboBox cbo)
{

long IdOut=-1;
if (cbo.SelectedItem==null)
return IdOut;
KeyValuePair<long, string> Item= (KeyValuePair<long, string>)cbo.SelectedItem;
IdOut   = Item.Key;
return IdOut;
}