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();
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.
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.