按基数 class 属性 过滤派生 class
Filter derived class by base class property
我有两个 classes (Car/Truck) 共享一个基础 class(汽车)。我想通过 属性 在其基础 class 汽车上过滤 Car 和 Truck 的集合。下面的代码会导致错误:
Cannot implicitly convert type
'System.Collections.Generic.List<Example.Program.Automobile>' to
'System.Collections.Generic.List<Example.Program.Car>'
Program.cs 48 27 Example
是否可以按基数 class 属性 进行过滤,而不必将结果转换回其适当的派生 class?
class Program
{
public class Automobile
{
public string Manufacturer { get; set; }
public static IEnumerable<Automobile> GetByManufacturer(IEnumerable<Automobile> items, string manufacturer)
{
return items.Where(o => o.Manufacturer == manufacturer);
}
}
public class Car : Automobile
{
public int TrunkSize { get; set; }
}
public class Truck : Automobile
{
public int BedSize { get; set; }
}
static void Main(string[] args)
{
var cars = new List<Car>
{
new Car { Manufacturer = "Toyota", TrunkSize = 100 },
new Car { Manufacturer = "Kia", TrunkSize = 70 }
};
var trucks = new List<Truck>
{
new Truck { Manufacturer = "Toyota", BedSize = 400 },
new Truck { Manufacturer = "Dodge", BedSize = 500 }
};
// Problem: Get a list of Type Car and a List of Tpye Truck,
// where each List contains only cars manufactured by Toyota
var mfr = "Toyota";
List<Car> toyotaCars = Automobile.GetByManufacturer(cars, mfr).ToList();
List<Car> toyotaTrucks = Automobile.GetByManufacturer(trucks, mfr).ToList();
Console.WriteLine(toyotaCars.First().GetType().Name);
Console.WriteLine(toyotaTrucks.First().GetType().Name);
}
}
您可以将定义更改为
public static IEnumerable<TAuto> GetByManufacturer(IEnumerable<TAuto> items, string manufacturer)
where TAuto : Automobile
{
return items.Where(o => o.Manufacturer == manufacturer);
}
现在您正在 return 生成 IEnumerable<Automobile>
,然后调用 ToList
将其转换为 List<Automobile>
,然后尝试将其转换为 [=15] =] 这是不合法的,因为列表 可能 包含 Automobile
而不是 Car
。
通过更改,您将 return 成为 IEnumerable<Car>
,它可以完全转换为 List<Car>
另外,第二次调用中的return类型应该是List<Truck>
,而不是List<Car>
:
List<Truck> toyotaTrucks = Automobile.GetByManufacturer(trucks, mfr).ToList();
我有两个 classes (Car/Truck) 共享一个基础 class(汽车)。我想通过 属性 在其基础 class 汽车上过滤 Car 和 Truck 的集合。下面的代码会导致错误:
Cannot implicitly convert type 'System.Collections.Generic.List<Example.Program.Automobile>' to 'System.Collections.Generic.List<Example.Program.Car>' Program.cs 48 27 Example
是否可以按基数 class 属性 进行过滤,而不必将结果转换回其适当的派生 class?
class Program
{
public class Automobile
{
public string Manufacturer { get; set; }
public static IEnumerable<Automobile> GetByManufacturer(IEnumerable<Automobile> items, string manufacturer)
{
return items.Where(o => o.Manufacturer == manufacturer);
}
}
public class Car : Automobile
{
public int TrunkSize { get; set; }
}
public class Truck : Automobile
{
public int BedSize { get; set; }
}
static void Main(string[] args)
{
var cars = new List<Car>
{
new Car { Manufacturer = "Toyota", TrunkSize = 100 },
new Car { Manufacturer = "Kia", TrunkSize = 70 }
};
var trucks = new List<Truck>
{
new Truck { Manufacturer = "Toyota", BedSize = 400 },
new Truck { Manufacturer = "Dodge", BedSize = 500 }
};
// Problem: Get a list of Type Car and a List of Tpye Truck,
// where each List contains only cars manufactured by Toyota
var mfr = "Toyota";
List<Car> toyotaCars = Automobile.GetByManufacturer(cars, mfr).ToList();
List<Car> toyotaTrucks = Automobile.GetByManufacturer(trucks, mfr).ToList();
Console.WriteLine(toyotaCars.First().GetType().Name);
Console.WriteLine(toyotaTrucks.First().GetType().Name);
}
}
您可以将定义更改为
public static IEnumerable<TAuto> GetByManufacturer(IEnumerable<TAuto> items, string manufacturer)
where TAuto : Automobile
{
return items.Where(o => o.Manufacturer == manufacturer);
}
现在您正在 return 生成 IEnumerable<Automobile>
,然后调用 ToList
将其转换为 List<Automobile>
,然后尝试将其转换为 [=15] =] 这是不合法的,因为列表 可能 包含 Automobile
而不是 Car
。
通过更改,您将 return 成为 IEnumerable<Car>
,它可以完全转换为 List<Car>
另外,第二次调用中的return类型应该是List<Truck>
,而不是List<Car>
:
List<Truck> toyotaTrucks = Automobile.GetByManufacturer(trucks, mfr).ToList();