2013-06-20 38 views
1

我已經創建了C#.net 4.0,它返回一個動態類型的方法:動態值4.0

public dynamic GetEntertainmentDetails(int entertainmentId) 
    { 
     dynamic result = from PE in entities.ProductEntertainments 
        join PM in entities.ProductModels on PE.ProductModelID equals PM.ProductModelID 
        join PMA in entities.ProductMasters on PM.ProductUID equals PMA.ProductUID 
        join PMF in entities.ProductManufactorers on PMA.ManufactorerID equals PMF.ManufactorerID 
        where PE.EntertainmentID == entertainmentId 
        select new { PE.EntertainmentID, PMF.ManufactorerID, PMA.ProductUID, PM.ProductModelID, PE.CDPlayer, PE.CDChanger, PE.DVDPlayer, PE.Radio, PE.AudioSystemRemoteControl, PE.SpeakersFront, PE.SpeakersRear }; 
     return result; 
    } 

我想使用的結果,其中是這樣的方法:

private void DisplayRecord() 
    { 
     dynamic item = dbContext.GetEntertainmentDetails(entertainmentId); 
     this.cmbManufacturer.SelectedValue = item.ManufactorerID; 
     this.cmbProducts.SelectedValue = item.ProductUID; 
     this.cmbVariant.SelectedValue = item.ProductModelID; 
     if (item.CDPlayer == true) 
      this.cdPlayerYes.IsChecked = true; 
     else 
      this.cdPlayerNo.IsChecked = true; 

    } 

但是返回的值沒有在cmbManufactorer或任何其他控件中設置。我測試了dyanmic項目有價值,但不知道爲什麼他們沒有被設置。

這是正確的方式來處理它,或者我在這裏做錯了嗎?

更新: 我在WPF應用程序中使用此代碼。 SelectedValue屬性可以在下拉菜單中獲取/設置。

+1

使用動態的任何理由?不能var關鍵字只是替換? LINQ通常用於編譯時安全 –

回答

1

你不能設置這樣的下拉列表中值自SelectedValue is a read only property
嘗試這樣的: -

// Assuming item.ManufactorerID is returning the index of selected item:- 

    cmbManufacturer.SelectedIndex = item.ManufactorerID; 

否則,如果你有下拉的value則: -

cmbManufacturer.Items.FindByText("PassedValue").Selected = true; 
+0

@Pranav ..如果item.ManufactorerID正在返回不在下拉列表中的某個值,則不會選擇該值(或默認值爲0),則可以在此處設置選定的值 –

+0

。用於測試只需檢查cmbManufacturer.SelectedIndex = 2; – Pranav

+0

是的,我100%確定返回值是在下拉列表中。這裏是另一個例子: VendData.GetProductColorsByProductColorID_Result colorObject = dbContext.GetProductColorsByID(productColorId); this.cmbManufacturer.SelectedValue = colorObject.ManufactorerID;這裏設置並顯示cmbManufacturer的值。 –

0

我不是確定你的item.ManufactorerID什麼樣的價值即將到來。

但是,動態應該工作,如果類型是compatable。

對於前:

dynamic s = "Item 3"; 
    drop1.SelectedValue = s; 

或者

dynamic s = 2; 
    drop1.SelectedIndex = s; 

兩個作品。

0

好吧,我得到了爲什麼它不起作用的原因。這是因爲GetEntertainmentDetails方法正在返回一個包含項目數組的複雜結果集,因此無法找到ManufacturerID或任何其他項目。我改變了代碼:

public dynamic GetEntertainmentDetails(int entertainmentId) 
    { 
     dynamic result = (from PE in entities.ProductEntertainments 
        join PM in entities.ProductModels on PE.ProductModelID equals PM.ProductModelID 
        join PMA in entities.ProductMasters on PM.ProductUID equals PMA.ProductUID 
        join PMF in entities.ProductManufactorers on PMA.ManufactorerID equals PMF.ManufactorerID 
        where PE.EntertainmentID == entertainmentId 
        select new 
        { 
         PE.EntertainmentID, 
         PMF.ManufactorerID, 
         PMA.ProductUID, 
         PM.ProductModelID, 
         PE.CDPlayer, 
         PE.CDChanger, 
         PE.DVDPlayer, 
         PE.Radio, 
         PE.AudioSystemRemoteControl, 
         PE.SpeakersFront, 
         PE.SpeakersRear 
        }).SingleOrDefault(); 
     return result; 
    } 

現在我可以綁定每個控件。