[英]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.