簡體   English   中英

LINQ實體框架協會

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM