Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于实体框架的LINQ关联_C#_Linq_Entity Framework_Associations - Fatal编程技术网

C# 基于实体框架的LINQ关联

C# 基于实体框架的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

我有一个连接到仪器的GUI。当我在GUI中选择仪器类型时,我希望从“InstType”表(仪器类型…)中获得相应的仪器id。然后将其存储到一个名为uu InstrumentType的变量中

我需要变量uuInstrumentType来返回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,它只返回一个元素(当集合包含更多元素时抛出异常)。试试这个:

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”???你有其他身份证吗?或者你有例外(哪种例外)?谢谢你的评论,我会更新原来的问题