C# 基于实体框架的LINQ关联
我有一个连接到仪器的GUI。当我在GUI中选择仪器类型时,我希望从“InstType”表(仪器类型…)中获得相应的仪器id。然后将其存储到一个名为uu InstrumentType的变量中 我需要变量uuInstrumentType来返回InstType的IDC# 基于实体框架的LINQ关联,c#,linq,entity-framework,associations,C#,Linq,Entity Framework,Associations,我有一个连接到仪器的GUI。当我在GUI中选择仪器类型时,我希望从“InstType”表(仪器类型…)中获得相应的仪器id。然后将其存储到一个名为uu InstrumentType的变量中 我需要变量uuInstrumentType来返回InstType的ID MyDataModelContainer FItype2 = new MyDataModelContainer(); //new instance of the entity container //retri
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,它只返回一个元素(当集合包含更多元素时抛出异常)。试试这个:
var list = (from IT in FItype2.InstType1
where IT.TypeName == _InstrumentType
select IT.Id).ToList();
在您的问题中,您已经标记出您对InstType ID感兴趣。我成功了。我换衣服的时候它就开始工作了
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();
林克仍处于早期阶段。它不会给你关于错误的详细反馈(或者在出现错误时甚至不会捕捉) 您想通过
TypeName
获取InstType
的ID
?什么是“我没有获取相应的InstType的ID”???你有其他身份证吗?或者你有例外(哪种例外)?谢谢你的评论,我会更新原来的问题