2011-12-06 115 views
4

當我嘗試在BuildTypes方法中投射投影列表時,我得到一個空值列表。我也試過使用.Cast(),但是我得到一個錯誤,一些屬性不能被轉換。如果有幫助,我可以發佈該錯誤。這裏是我的代碼:使用LINQ投射投影列表返回空值列表?

public class AuditActionType: EntityValueType 
{ 
} 

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType 
{ 
    var types = 
     (from ty in xDocument.Descendants("RECORD") 
     select new 
      { 
       Id = GenerateGuid(), 
       Name = ty.Element("Name").Value, 
       EntityStatus = _activeEntityStatus, 
       DateCreated = DateTime.Now, 
       DateModified = DateTime.Now 
      } as T).ToList(); 

    return types; 
} 

所以,我只能說這是這樣的:

var auditActorTypes = BuildTypes<AuditActorType>(auditActorTypesXml) 

我有一噸重的類型,我需要從一個XML文件中提取,並沒有想複製代碼每種類型。

回答

7

您正在嘗試將類型爲T的匿名對象強制轉換爲無法完成的操作。匿名類型是它自己獨特的類型,絕不涉及T傳入。

相反,你可以在T型供應new()約束意味着它需要一個默認的構造,然後執行new T()而不是創建的一個新的匿名類型:

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType, new() 
{ 
    var types = 
     (from ty in xDocument.Descendants("RECORD") 
     select new T() 
      { 
       Id = GenerateGuid(), 
       Name = ty.Element("Name").Value, 
       EntityStatus = _activeEntityStatus, 
       DateCreated = DateTime.Now, 
       DateModified = DateTime.Now 
      }).ToList(); 

    return types; 
} 

這是假設,當然,前提是IdNameEntityStatusDateCreated,並DateModified是基本EntityValueType的所有屬性。

+0

+1毆打我給它。可能值得一提的是,'Id' /'Name'/etc需要存在於'EntityValueType'上。 –

+0

@ J.Kommer:好點。 –

1

更改代碼:

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType, new() 
{ 
    var types = 
     (from ty in xDocument.Descendants("RECORD") 
     select new T() 
      { 
       Id = GenerateGuid(), 
       Name = ty.Element("Name").Value, 
       EntityStatus = _activeEntityStatus, 
       DateCreated = DateTime.Now, 
       DateModified = DateTime.Now 
      }).ToList(); 

    return types; 
} 
+1

你需要新的()約束條件 –

+0

@James Michael Hare,@ DDiVita - 是的,我誤以爲你的觀點。 –

+0

@RedHat:不用擔心,這是一個小小的贅肉,我以爲你的意思是:-)我現在會+1。 –

4

你不能用你當前的代碼爲new { }爲此創建一個anonymous type其中有以T沒有關係(既不是孩子,也不是T型的吧)。你可以做的,而不是在你的EntityValueType類和變革實施IdNameEntityStatusDateCreatedDateModified作爲屬性:

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType 

要:

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType, new() 

其中規定,任何類型的參數傳遞給我們的方法必須有一個無參數的構造函數,它允許通過改變來實際構造一個類型爲T的對象:

select new { ... } as T 

要:

select new T { ... } 

最終結果:

public class EntityValueType 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    // Change this to the correct type, I was unable to determine the type from your code. 
    public string EntityStatus { get; set; } 
    public DateTime DateCreated { get; set; } 
    public DateTime DateModified { get; set; } 
} 

public class AuditActionType: EntityValueType 
{ 
} 

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType, new() 
{ 
    return (from ty in xDocument.Descendants("RECORD") 
     select new T 
      { 
       Id = GenerateGuid(), 
       Name = ty.Element("Name").Value, 
       EntityStatus = _activeEntityStatus, 
       DateCreated = DateTime.Now, 
       DateModified = DateTime.Now 
      }).ToList(); 
}