我现在对数组比较和创建它感到困惑。我有 2 张桌子,车站和位置。站内的列是 id 和站名。位置表相同,唯一不同的是名称。我将 id 放入数组中,并将名称放入数组中。我想比较名称和 id,以便当用户选择名称时,程序知道哪个 id 属于所选名称,并使用它们的主键将其保存到另一个表中。这是我从现在开始创建的代码,但我不知道如何解决它。希望我能在这里得到一些帮助。谢谢!
private void btnCreate_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
string[] stations = StationNameList();
int[] stationsID = StationIDList();
string[] locations = LocationNameList();
int[] locationsID = LocationIDList();
int Stationindex = cbStation.SelectedIndex;
int Locationindex = cbLocation.SelectedIndex;
trolleyservice TS = new trolleyservice();
TS.stationID = cbStation.SelectedIndex;
TS.locationID = cbLocation.SelectedIndex;
TS.tServiceTiming = txtTime.Text;
TS.tServiceType = txtType.Text;
Setupctx.trolleyservices.AddObject(TS);
txtTime.Text = "";
txtType.Text = "";
MessageBox.Show("New Trolley Service Has Been Created.");
}
}
这是我为每个表创建的所有数组。
private string[] StationNameList()
{
using (testEntities Setupctx = new testEntities())
{
return Setupctx.stations.Select(s => s.Station1).OrderBy(s => s).ToArray();
}
}
private int[] StationIDList()
{
using (testEntities Setupctx = new testEntities())
{
return Setupctx.stations.Select(sid => sid.idstations).OrderBy(sid => sid).ToArray();
}
}
private string[] LocationNameList()
{
using (testEntities Setupctx = new testEntities())
{
return Setupctx.locations.Select(l => l.Location1).OrderBy(l => l).ToArray();
}
}
private int[] LocationIDList()
{
using (testEntities Setupctx = new testEntities())
{
return Setupctx.locations.Select(lid => lid.idlocation).OrderBy(lid => lid).ToArray();
}
}