Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ强制转换投影列表是否返回空值列表?_C#_Linq_Linq To Xml - Fatal编程技术网

C# 使用LINQ强制转换投影列表是否返回空值列表?

C# 使用LINQ强制转换投影列表是否返回空值列表?,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,当我尝试在BuildTypes方法中强制转换投影列表时,我得到一个空值列表。我也尝试过使用.Cast(),但出现了一个错误,即某些属性无法强制转换。如果有帮助,我可以发布错误。这是我的密码: public class AuditActionType: EntityValueType { } private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType { var types

当我尝试在BuildTypes方法中强制转换投影列表时,我得到一个空值列表。我也尝试过使用.Cast(),但出现了一个错误,即某些属性无法强制转换。如果有帮助,我可以发布错误。这是我的密码:

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;
} 
公共类AuditActionType:EntityValueType
{
}
私有列表构建类型(XDocument XDocument),其中T:EntityValueType
{
变量类型=
(摘自xDocument.subjects(“记录”)中的ty)
选择新的
{
Id=GenerateGuid(),
Name=ty.Element(“Name”).Value,
EntityStatus=\u activeEntityStatus,
DateCreated=DateTime。现在,
DateModified=DateTime.Now
}as T)ToList();
返回类型;
} 
所以我会这样称呼它:

var auditActorTypes = BuildTypes<AuditActorType>(auditActorTypesXml)
var auditActorTypes=BuildTypes(auditActorTypesXml)

我需要从XML文件中提取大量类型,并且不想为每种类型重复代码。

您正试图将匿名对象强制转换为type
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;
} 
私有列表构建类型(XDocument XDocument),其中T:EntityValueType,new()
{
变量类型=
(摘自xDocument.subjects(“记录”)中的ty)
选择新的T()
{
Id=GenerateGuid(),
Name=ty.Element(“Name”).Value,
EntityStatus=\u activeEntityStatus,
DateCreated=DateTime。现在,
DateModified=DateTime.Now
}).ToList();
返回类型;
} 
当然,这是假设
Id
Name
EntityStatus
DateCreated
DateModified
都是基
EntityValueType
的属性更改代码:

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;
} 
私有列表构建类型(XDocument XDocument),其中T:EntityValueType,new()
{
变量类型=
(摘自xDocument.subjects(“记录”)中的ty)
选择新的T()
{
Id=GenerateGuid(),
Name=ty.Element(“Name”).Value,
EntityStatus=\u activeEntityStatus,
DateCreated=DateTime。现在,
DateModified=DateTime.Now
}).ToList();
返回类型;
} 

您不能对当前代码执行此操作,因为
新建{}
创建了一个与T无关的代码(它既不是子代码,也不是T类型)。相反,您可以将
Id
Name
EntityStatus
DateCreated
DateModified
实现为
EntityValueType
类的属性并更改:

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

最终结果:

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();
} 
公共类EntityValueType
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
//将此更改为正确的类型,我无法根据您的代码确定类型。
公共字符串EntityStatus{get;set;}
public DateTime DateCreated{get;set;}
公共日期时间日期修改{get;set;}
}
公共类AuditActionType:EntityValueType
{
}
私有列表构建类型(XDocument XDocument),其中T:EntityValueType,new()
{
返回(来自xDocument.subjects(“记录”)中的ty)
选择新的T
{
Id=GenerateGuid(),
Name=ty.Element(“Name”).Value,
EntityStatus=\u activeEntityStatus,
DateCreated=DateTime。现在,
DateModified=DateTime.Now
}).ToList();
} 

您需要新的()constraint@James迈克尔·黑尔,@DDiVita-是的,我弄错了,谢谢你的观点。@RedHat:别担心,这是一个小失误,我想你是认真的:-)我现在会+1耶。+1谢谢你打败我。可能值得一提的是,
Id
/
Name
/etc需要存在于
EntityValueType
上。
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();
}