Ujae Kang
Ujae Kang

Reputation: 167

Association on LINQ on Entity Framework

enter image description hereenter image description hereenter image description here

I have a GUI that is connected to Instrument. When I select an instrument type in GUI, I want the corresponding instrument id from "InstType" table(Instrument type...). Then store it into a variable called __InstrumentType.

I need the variable __InstrumentType to return the InstType's 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();

Upvotes: 0

Views: 51

Answers (2)

Ujae Kang
Ujae Kang

Reputation: 167

I got it to work. It started working when I changed

var list = (from IT in FItype2.InstType1
            where IT.TypeName == _InstrumentType
            select IT.Id).Last();

to

var list = (from IT in FItype2.InstType1
            where IT.TypeName == _InstrumentType
            select IT.Id).Max();

LINQ is still in the early stage. It won't give you detailed feed back on errors (or don't even catch when there is an error).

Upvotes: 0

mr100
mr100

Reputation: 4428

If you want to get a list you shouldn't use Single - it returns only one element (throws exception when collection contains more). Try this:

var list = (from IT in FItype2.InstType1
                    where IT.TypeName == _InstrumentType
                    select IT.Id).ToList();

In your question you've marked that you are interested in InstType Ids.

Upvotes: 1

Related Questions