分类时如何铸造?[英] How to cast while Sorting?

本文是小编为大家收集整理的关于分类时如何铸造?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有类车辆和类:CAR 从类中继承:车辆如下所示.我有一个列表,我想根据汽车类的属性(不是车辆父级)使用 linq 进行排序.

class vehicle
{
    public String Name {get; set; }
}

class Car:Vehicle
{
    public String ModelName {get; set; }
}

List<Vehicle> vehicleList=new List<Vehicle>();

Car c1 = new Car();
vechicleList.Add(c1); //s note that i am adding **Car** objects

Car c2 = new Car();
vechicleList.Add(c2);
// added 10 such Car objects

然后,我想根据Carmodel(这是汽车的属性,而不是父级的属性)对车辆级排序

我尝试了以下一个,但不起作用.

vehicleList.OrderBy(c => (Car)c.ModelName)

关于如何执行此操作的任何帮助?

推荐答案

最可读的代码,您想要的是

vehicleList.Cast<Car>().OrderBy(c => c.ModelName);

虽然您当然也可以修复Parens并写入与

相同
vehicleList.OrderBy(c => ((Car)c).ModelName);

也就是说,如果您的列表中有任何不是Car> 的Vehicle,则以上两个都会炸毁.如果那不会发生,那为什么不首先是List<Car>?

其他推荐答案

您必须决定要拥有的内容:

Car实例列表或Vehicle实例列表.

如果您想拥有Vehicle实例列表,则不能按Car的属性订购,因为在Vehicles的列表中,也可能有Bus或a Bicycle. 做您当前尝试的操作(铸造到Car)可能会在运行时抛出异常.

说,如果您坚持这样做,则必须意识到两件事:

  1. 您需要修复演员阵容:vehicleList.OrderBy(c => ((Car)c).ModelName)
  2. 您需要意识到OrderBy不执行现场排序. vehicleList仍将处于原始订购中.如果要在vehicleList中以订购结果的订购结果,则需要将OrderBy的结果分配给它:

    vehicleList = vehicleList.OrderBy(c => ((Car)c).ModelName).ToList();
    

其他推荐答案

您必须施放c c,而不是c.ModelName.

尝试:

vehicleList.OrderBy(c => ((Car)c).ModelName)

本文地址:https://www.itbaoku.cn/post/1557152.html

问题描述

I have classes Vehicle and class:Car is inherited from class:Vehicle as in below. I have a List which I want to sort using LINQ based on a property of Car class (not of vehicle parent class).

class vehicle
{
    public String Name {get; set; }
}

class Car:Vehicle
{
    public String ModelName {get; set; }
}

List<Vehicle> vehicleList=new List<Vehicle>();

Car c1 = new Car();
vechicleList.Add(c1); //s note that i am adding **Car** objects

Car c2 = new Car();
vechicleList.Add(c2);
// added 10 such Car objects

Then I want to Sort vehicleList based on CarModel (which is a property of Car, not of Parent class)

I tried the below one but it does not work.

vehicleList.OrderBy(c => (Car)c.ModelName)

Any help on how to do this?

推荐答案

The most readable code that does what you want is

vehicleList.Cast<Car>().OrderBy(c => c.ModelName);

while of course you could also fix the parens and write the same as

vehicleList.OrderBy(c => ((Car)c).ModelName);

That said, both of the above will blow up if there is any Vehicle in your list that is not a Car. And if that can't happen, then why isn't that a List<Car> in the first place?

其他推荐答案

You have to decide what you want to have:

A list of Car instances or a list of Vehicle instances.

If you want to have a list of Vehicle instances you can't order by properties of Car, because in a list of Vehicles there also could be a Bus or a Bicycle.
Doing what you currently try (casting to Car) will possibly throw an exception at runtime.

Having said that, if you insist on doing it, you have to be aware of two things:

  1. You need to fix your cast: vehicleList.OrderBy(c => ((Car)c).ModelName)
  2. You need to be aware that OrderBy doesn't perform an in-place sort. vehicleList will still be in its original ordering. If you want to have the ordered result in vehicleList, you need to assign the result of OrderBy to it:

    vehicleList = vehicleList.OrderBy(c => ((Car)c).ModelName).ToList();
    

其他推荐答案

You have to cast c into a Car, not c.ModelName.

Try :

vehicleList.OrderBy(c => ((Car)c).ModelName)