2011-12-06 64 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(); 
}