您可以使用以下代码获取ClassX
对象列表。
var list = (from t in listY
select new ClassX
{
id = t.id,
title = t.title,
description = t.description
}).ToList();
但是你可以ClassY
作为子类来ClassX
使用继承
具有以下类结构:
public class ClassX
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
}
public class ClassY : ClassX
{
public int localeId { get; set; }
public int clientId { get; set; }
}
稍后您可以执行以下操作:
List<ClassY> listY = new List<ClassY>();
listY.Add(new ClassY { id = 0, title = "sometitle1", description = "some description", localeId = 1, clientId = 2 });
listY.Add(new ClassY { id = 1, title = "sometitle2", description = "some description", localeId = 1, clientId = 2 });
listY.Add(new ClassY { id = 2, title = "sometitle3", description = "some description", localeId = 1, clientId = 2 });
List<ClassX> listX = (from t in listY
select (ClassX)t).ToList(); //casting to base class
您将项目ClassY
转换为基类ClassX