[英]Association on LINQ on Entity Framework
我有一个与仪器连接的GUI。 当我在GUI中选择一种乐器类型时,我希望从“ InstType”表(乐器类型...)中获得相应的乐器ID。 然后将其存储到名为__InstrumentType的变量中。
我需要变量__InstrumentType来返回InstType的ID。
MyDataModelContainer FItype2 = new MyDataModelContainer(); //new instance of the entity container
//retrieves a string type data from GUI's combobox
string _InstrumentType = this.comboBox_InstType.GetItemText(comboBox_InstType.SelectedItem);
//create a variable
int __InstrumentType=0;
var list = (from IT in FItype2.InstType1
where IT.TypeName == _InstrumentType
select IT.Id).Last();
如果要获取列表,则不应使用Single-它仅返回一个元素(当collection包含更多元素时抛出异常)。 尝试这个:
var list = (from IT in FItype2.InstType1
where IT.TypeName == _InstrumentType
select IT.Id).ToList();
在您的问题中,您已标记您对InstType Ids感兴趣。
我知道了。 当我改变时它开始工作
var list = (from IT in FItype2.InstType1
where IT.TypeName == _InstrumentType
select IT.Id).Last();
至
var list = (from IT in FItype2.InstType1
where IT.TypeName == _InstrumentType
select IT.Id).Max();
LINQ仍处于早期阶段。 它不会为您提供有关错误的详细反馈(或者在出现错误时甚至无法捕获)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.