2011-09-08 141 views
1

我有一個方法應該返回一個Rpt_IncidentWithConfirm對象,但我不知道如何輕鬆地將其轉換爲一個。我知道它是如何做的唯一方法就是做到我在下面做的是非常低效的。匿名類型轉換爲名義類型

public Rpt_IncidentWithConfirm GetIncident(string IncidentID) 
    { 
     db = new IncidentsDataContext(); 

     var incident = (from i in db.Rpt_IncidentWithConfirms 
         join d in db.DropDowns on i.incidentType equals d.value 
         where i.incidentID == Convert.ToInt32(IncidentID) 
         select new 
         { 
          i, d.text 
         }).SingleOrDefault(); 
     Rpt_IncidentWithConfirm r = new Rpt_IncidentWithConfirm(); 
     // I didn't want to have to type all this here because I have too many fields to map. 

     r.bhaIncident = incident.i.bhaIncident; 
     r.bitType = incident.i.bitType; 
     r.Bottom_Connection = incident.i.Bottom_Connection; 
     // And so on. 


     return r; 
    } 
+2

匿名類型如下命名類型爲強,他們只是沒有一個名稱而已 –

+0

@Rune良好的出發點。它通常是*名義*與匿名類型。 – dlev

+0

什麼「非常低效」? –

回答

1

我實際上使用了下面的鏈接來解決我的問題。這不完全是我想要做的,但我現在不必手動輸入所有內容。 Return anonymous type results?

 public class IncidentWithDropDown 
    { 
     public Rpt_IncidentWithConfirm Incident { get; set; } 
     public string IncidentTypeText { get; set; } 
    } 
    public IncidentWithDropDown GetIncident(string IncidentID) 
    { 
     db = new IncidentsDataContext(); 

     var incident = (from i in db.Rpt_IncidentWithConfirms 
         join d in db.DropDowns on i.incidentType equals d.value 
         where i.incidentID == Convert.ToInt32(IncidentID) 
         select new IncidentWithDropDown() 
         { 
          Incident = i, 
          IncidentTypeText = d.text 
         }).SingleOrDefault(); 

     return incident; 
    } 
+0

小的更新,你可以在使用初始值設定項時刪除構造函數():'select new IncidentWithDropDown {...}' –

3

您可以在查詢表達式直接實例Rpt_IncidentWithConfirm對象,只是指你所需要的數據庫值:

var incident = (from i in db.Rpt_IncidentWithConfirms 
       join d in db.DropDowns on i.incidentType equals d.value 
       where i.incidentID == Convert.ToInt32(IncidentID) 
       select new Rpt_IncidentWithConfirm 
       { 
        bhaIncident = i.bhaIncident 
       , bitType = i.bitType 
       , Bottom_Connection = i.Bottom_Connection 
       }).SingleOrDefault(); 
+0

這不會解決我現在的問題。我試圖找到一種方法來避免做「bhaIncident = i.bhaIncident」,因爲我需要數據庫中的每一列。 – MattAitchison

2

不要使用anonymus type你可以使用你的類型返回

select new Rpt_IncidentWithConfirm 
        { 
         // set all properties you need 
        }).SingleOrDefault(); 

編輯: 如果您的查詢是關於您想返回的集合類型,您可以簡單地使用查詢結果:

return db.Rpt_IncidentWithConfirms.Where(...).FirstOrDefault(); 

,或者如果u需要的文字應用價值:

//do something with incident.Text 
return incident.i; 
0

則無需在選擇創建一個匿名類型只需要創建一個命名類型,而不是

public Rpt_IncidentWithConfirm GetIncident(string IncidentID) 
    { 
     db = new IncidentsDataContext(); 

     var return (from i in db.Rpt_IncidentWithConfirms 
         join d in db.DropDowns on i.incidentType equals d.value 
         where i.incidentID == Convert.ToInt32(IncidentID) 
         select new Rpt_IncidentWithConfirm(){ 

          bhaIncident =i.bhaIncident, 
          bitType = i.bitType, 
          Bottom_Connection = i.Bottom_Connection, 
          // And so on. 
         }).SingleOrDefault() 
    } 
的對象