2015-02-09 115 views
0

我想從數據庫獲取一些數據並將它們綁定在Drop -down名單。但越來越以下錯誤: -不能隱式地將類型'System.Collections.Generic.List <AnonymousType#1>'轉換爲'System.Collections.Generic.List <DAL.HRM_PersonalInformations''

public virtual List<HRM_PersonalInformations>GetAssignePerson(String OCODE, int dptID){ 
var query = (context.HRM_PersonalInformations.Where 
      (c => c.OCODE == OCODE && c.DepartmentId == dptID) 
      .Select(c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID})).ToList(); 

    return query; // It indicate error line 
} 

在那之後我試圖綁定在下拉列表中的數據和我的代碼如下: -

private void FillAssignPerson() 
{ 
    try 
    { 
      string OCODE = ((SessionUser)Session["SessionUser"]).OCode; 
      int dptId = Convert.ToInt32(ddlAssignPerDept.SelectedValue); 

      var row = enquiryBll.GetAssignePerson(OCODE, dptId).ToList(); 

      //const string list = "SELECT FirstName + ' ' + LastName AS FullName, EID, FirstName, LastName " + 
      //     "FROM HRM_PersonalInformations " + 
      //     "ORDER BY EID ASC" 
      if (row.Count > 0) 
      { 
       ddlAssignPerson.Items.Clear(); 
       ddlAssignPerson.DataSource = row; 
       ddlAssignPerson.DataTextField = "FullName"; 
       ddlAssignPerson.DataValueField = "EID"; 
       ddlAssignPerson.DataBind(); 
       ddlAssignPerson.Items.Insert(0, new ListItem("----- Select One -----", "0")); 
       ddlAssignPerson.AppendDataBoundItems = false; 
      } 
     } 

是不是正確的方式?誰能幫我 ?感謝提前..

回答

1

那麼,在你的投影你有:

c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID} 

這是創建一個匿名typoe的實例 - 不是HRM_PersonalInformations一個實例。如果這些是你在HRM_PersonalInformations需要的只有兩個屬性,你可以只將其更改爲:

c => new HRM_PersonalInformations { 
    FullName = (c.FirstName + ' ' + c.LastName), 
    EID = c.EID 
} 

或者通過查詢來判斷,你可能不需要投影在所有。你可能會罰款:

return context.HRM_PersonalInformations 
       .Where(c => c.OCODE == OCODE && c.DepartmentId == dptID) 
       .ToList(); 

另外,如果你只需要那些性質並不真的需要你的列表中的項目是HRM_PersonalInformations情況下,你可以只改變方法的簽名是:

public virtual IList GetAssignePerson(...) 

並保持原樣。你不能明確地編寫一個指定匿名類型的方法聲明,因爲它沒有名字 - 但是List<T>實現了IList,所以上面的代碼肯定會編譯...如果你稍後嘗試在它到HRM_PersonalInformations雖然。

編輯:如果字符串連接是一個問題,您可能需要做那個客戶端。例如:

public IList GetAssignePerson(String OCODE, int dptID) { 
    return context.HRM_PersonalInformations 
        .Where(c => c.OCODE == OCODE && c.DepartmentId == dptID) 
        .Select(c => new { c.FirstName, c.LastName, c.EID }) 
        .AsEnumerable() // Do the rest client-side 
        .Select(c => new { 
         FullName = c.FirstName + " " + c.LastName, 
         c.EID 
        }) 
        .ToList(); 
} 
+0

我已經嘗試你的第一個提供方法......那個時候它不承認「全名」屬性.... 當我嘗試你最後的提供方法和程序執行後,給我跟着錯誤.. 「{」無法創建一個'System.Object'類型的常量值。 「}」 – mgsdew 2015-02-09 08:50:20

+0

@mgsdew:WellName是'HRM_PersonalInformations'屬性的'FullName'嗎?我們不知道這種類型是什麼樣子。我不清楚最後一種情況是什麼問題 - 可能是由於字符串連接,但很難說清楚。 – 2015-02-09 08:51:44

+0

@mgsdew:看到我的編輯另一種選擇。 – 2015-02-09 08:54:00

0

此方法正在等待您HRM_PersonalInformations,爲什麼您返回匿名類型?

它回到預期的類型

試試這個代碼

.Select(c => new HRM_PersonalInformations() 
{ 
    FullName = c.FirstName 
    // set other prop 
}); 
+0

我已經嘗試過這種方式...那次它不能識別'FullName'字段......因爲在我的表格中沒有'FullName'字段, ..只是它包含'FirstName'和'LastName'.. – mgsdew 2015-02-09 08:29:16

+0

數據模型應該只用於保存,更新過程。 您可以創建業務模型HRM_PersonalInformationsModel並添加FullName prop。 您回到GetAssignePerson方法的商業模式 – 2015-02-09 08:37:45

+0

是的。 HRM_PersonalInformation一個包含'FirstName'和'LastName'屬性的表格..現在我希望FullName在下拉列表中顯示數據... 如何創建業務模型?我的意思是創建商業模式的過程。 – mgsdew 2015-02-09 08:47:03

0

這是一個簡單的示例,

如果你想定製數據業務流程或UI過程中,你是有用需要一個新的模型,例如你想在下拉顯示全名,所以,

  • 添加新的fol DER MyCustomizeModels
  • 在MyCustomizeModels

這DropdownLookUpDecimal類添加新類DropdownLookUpDecimal。

public class DropdownLookUpDecimal 
    { 
     public decimal Value { get; set; } 
     public string Text { get; set; } 

    }  

你可以使用這個類的所有下拉項目。

public List<DropdownLookUpDecimal>GetAssignePerson(string oCode, int dptID) 
{ 
     var query = context.HRM_PersonalInformations.Where 
      (c => c.OCODE == oCode&& c.DepartmentId == dptID) 
      .Select(c => new DropdownLookUpDecimal 
       { 
        Text = c.FirstName + ' ' + c.LastName, 
        Value = c.EID 
       });  
    return query.ToList(); 
} 

只需創建新的.cs和填充類列表,並返回列表。

我希望它幫

相關問題