我遇到了一些问题,当用户在组合框中选择所选名称时,与所选名称链接的数据将显示在列表框中。我在使用这种方法时遇到问题。错误就在这里。
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
错误:无法将类型为“<>f_AnonymousType2'2 [System.String.System.Int32]”的对象转换为类型“System.IConvertible”。
private void cbLocStation_SelectedIndexChanged(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
var query = (from db in Setupctx.requiredtimings
join timing t in Setupctx.timings on db.RequiredTimingID equals t.TimingID
where db.RequiredLocationStationID == selectLocStation
select new
{
t.Time2
}).ToList();
List<TimeSpan> lstSelectedTime = new List<TimeSpan>();
foreach (var a in query)
{
lstSelectedTime.Add((TimeSpan)a.Time2);
}
lstTime.DataSource = lstSelectedTime;
}
}
这就是我在组合框中存储数据的方法。
using (satsEntities Setupctx = new satsEntities())
{
var DeleteRT = (from DelRT in Setupctx.requiredtimings
join locationstationname ls in Setupctx.locationstationnames on DelRT.RequiredLocationStationID equals ls.locationstationID
select new { ls.locStatname, DelRT.RequiredLocationStationID }).Distinct().ToList();
cbLocStation.DataSource = DeleteRT.ToList();
cbLocStation.DisplayMember = "locStatname";
cbLocStation.ValueMember = "RequiredLocationStationID";
任何帮助将不胜感激。