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 Entities - Fatal编程技术网

C# 使用LINQ将两个实体合并为一个实体

C# 使用LINQ将两个实体合并为一个实体,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我试图将我的两个不同实体合并成一个新实体。下面是我的类实体的一个示例: public class CarOne { public string Name { get; set; } public string Model { get; set; } } public class CarTwo { public int Year { get; set; } public string Descr

我试图将我的两个不同实体合并成一个新实体。下面是我的类实体的一个示例:

public class CarOne
    {
        public string Name { get; set; }

        public string Model { get; set; }

    }

    public class CarTwo
    {
        public int Year { get; set; }

        public string Description { get; set; }  

    }
现在,我想将我的两个实体列表保存到这个新实体中:

public class CarFinal
    {
        public string Name { get; set; }

        public string Model { get; set; }

        public int Year { get; set; }

        public string Description { get; set; }  

    }
以下是我的代码示例:

        CarOne carToyota = new CarOne()
        {
            Name = "Toyota",
            Model = "Camry"
        };

        CarTwo carDetails = new CarTwo()
        {
           Year = 2012,
           Description = "This is a great car"
        };

        List<CarOne> lstFirst = new List<CarOne>();
        lstFirst.Add(carToyota);

        List<CarTwo> lstSecond = new List<CarTwo>();
        lstSecond.Add(carDetails);
但是这两个方法的输出都会产生两个元素,我只是想把所有属性合并成一个元素。我只期望一个实体作为结果,但我总是在我的组合中得到两个元素

像这样使用Zip:

var finalList = lstFirst.Zip(lstSecond, (c1, c2) => new CarFinal()
        {
            Name = c1.Name,
            Model = c1.Model,
            Description = c2.Description,
            Year = c2.Year
        }).ToList();
像这样使用Zip:

var finalList = lstFirst.Zip(lstSecond, (c1, c2) => new CarFinal()
        {
            Name = c1.Name,
            Model = c1.Model,
            Description = c2.Description,
            Year = c2.Year
        }).ToList();

使用方法前请先阅读相关内容!使用方法前请先阅读相关内容!
var finalList = lstFirst.Zip(lstSecond, (c1, c2) => new CarFinal()
        {
            Name = c1.Name,
            Model = c1.Model,
            Description = c2.Description,
            Year = c2.Year
        }).ToList();