简体   繁体   English

无法将dto列表转换为动态列表

[英]Cannot convert list of dto to list of dynamic

How can I make this to work? 我该如何运作? It giving me an error cannot implicitly convert . 它给我一个错误,无法implicitly convert I have lots of DTO on my WebApp and I need a placeholder variable. 我的WebApp上有很多DTO,并且需要一个占位符变量。

NOTE: This is not the actual code on my WebApp, this is just the same idea on what I want to do. 注意:这不是WebApp上的实际代码,这与我要执行的操作相同。

public class DTO1
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DTO2
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DTO1Service
{
    public static List<DTO1> GetListOfDTO1()
    {
        return new List<DTO1>
        {
            new DTO1 { Id = 1, Name = "DTO 1" },
            new DTO1 { Id = 2, Name = "DTO 2" }
        };
    }
}

public class DTO2Service
{
    public static List<DTO2> GetListOfDTO2()
    {
        return new List<DTO2>
        {
            new DTO2 { Id = 1, Name = "DTO 1" },
            new DTO2 { Id = 2, Name = "DTO 2" }
        };
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var entities = new List<dynamic>();

        var serviceType = Console.ReadLine();

        if(serviceType == "1")
            entities = (dynamic)DTO1Service.GetListOfDTO1();
        else if (serviceType == "2")
            entities = (dynamic)DTO2Service.GetListOfDTO2();

        Console.ReadLine();
    }
}

You can use Cast method to explicitly cast it to dynamic , like so: 您可以使用Cast方法将其显式转换为dynamic ,如下所示:

if(serviceType == "1")
        entities = DTO1Service.GetListOfDTO1().Cast<dynamic>().ToList();
else if (serviceType == "2")
        entities = DTO2Service.GetListOfDTO2().Cast<dynamic>().ToList();

You are casting it wrong. 你错了。 You are trying to assign a dynamic to a List<dynamic> instead of the list members 您试图将dynamic分配给List<dynamic>而不是列表成员

You can either create a new instance and pass the values in 您可以创建一个新实例并在其中传递值

if(serviceType == "1")
    entities = new List<dynamic>(DTO1Service.GetListOfDTO1());
else if (serviceType == "2")
    entities = new List<dynamic>(DTO2Service.GetListOfDTO2());

or just populate the initially created instance 或只是填充最初创建的实例

if(serviceType == "1")
    entities.AddRange(DTO1Service.GetListOfDTO1());
else if (serviceType == "2")
    entities.AddRange(DTO2Service.GetListOfDTO2());

I personally prefer the second option as you have already initialized the variable and would just be populating it instead of creating an instance just to reassign it after. 我个人更喜欢第二个选项,因为您已经初始化了变量,并且只会填充它,而不是创建实例以在之后重新分配它。

Based on the comment on the question, I am under the impression that OP needed something like that: 基于对问题的评论,我觉得OP需要这样的东西:

public interface IAmDTO {

    int Id { get; set; }

    string Name { get; set; }
}

public class DTO1 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO2 : IAmDTO {

    public int Id { get; set; }

    public string Name { get; set; }
}

public class DTO1Service {

    public static List<DTO1> GetListOfDTO1() =>
        new List<DTO1>
        {
        new DTO1 { Id = 1, Name = "DTO 1" },
        new DTO1 { Id = 2, Name = "DTO 2" },
        };
}

public class DTO2Service {

    public static List<DTO2> GetListOfDTO2() =>
        new List<DTO2>
        {
        new DTO2 { Id = 1, Name = "DTO 1" },
        new DTO2 { Id = 2, Name = "DTO 2" },
        };
}
}

public static class DTOExtensions {

public static void DtoHelper(this IEnumerable<IAmDTO> dtoS) {
    foreach(var dto in dtoS) {
        //DO SOMETHING WITH
        var id = dto.Id;
        var name = dto.Name;
    }

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM