1

这似乎很困难,并且希望能提供任何帮助或帮助。

我有一个清单,清单。每个 Car 对象都有一个列表,List。我想按其中 1 个配件对我的汽车清单进行排序。在我的示例中,我保证有几个带有我要查找的附件的 Car 对象。

现在我知道了附件名称,但我不知道用于根据附件名称对我的汽车列表进行排序的语法。

  • AccessoryObj1 => 名称(字符串:动态转向)
  • AccessoryObj2 => 名称(字符串:侧面辅助)

  • Car1 -> 列表(AccessoryObj1、AccessoryObj2、AccessoryObj3)

  • Car2 -> 列表(AccessoryObj1、AccessoryObj2、AccessoryObj3)

列表(汽车 1、汽车 2)

我尝试的语法如下:

String sortField = "Side Assist";
List<Car> sortedList = sortedList.OrderBy(
                        x => x.Accessories.OfType<Accessory>().ToList().First(y => y.Name == sortField)).ToList();

我试图通过引用每个汽车对象的附件列表来排序(升序)我的汽车列表,并在附件列表中获取附件对象名称与 sortField 值匹配的第一个匹配项。

不幸的是,这种尝试会导致以下错误:

至少一个对象必须实现 IComparable

我希望我的问题足够清楚,并提前感谢。


感谢 user2399170 提供的解决方案,我是那里的 3/4:拿他们的解决方案/样本,我现在正在尝试将最终结果排序为升序/降序。这是调整后的代码:

public class Accessory
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Car
{
    public string Name { get; set; }
    public IList<Accessory> Accessories { get; set; }
}

private IEnumerable<Car> GetCars()
    {
        var cars = new List<Car>();
        cars.Add(
            new Car()
            {
                Name = "CAR1",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "1" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "0" },
                    new Accessory() { Name = "Sat/Nav", Value = "1" },
                    new Accessory() { Name = "Side Assist", Value = "0" },
                    new Accessory() { Name = "Name", Value = "BMW" },
                }
            });
        cars.Add(
            new Car()
            {
                Name = "CAR2",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "0" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "0" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "0" },
                    new Accessory() { Name = "Sat/Nav", Value = "0" },
                    new Accessory() { Name = "Side Assist", Value = "0" },
                    new Accessory() { Name = "Name", Value = "Mazda" },
                }
            });
        cars.Add(
            new Car()
            {
                Name = "CAR3",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "1" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "1" },
                    new Accessory() { Name = "Sat/Nav", Value = "1" },
                    new Accessory() { Name = "Side Assist", Value = "1" },
                    new Accessory() { Name = "Name", Value = "Audi" },

                }
            });

        String[] sortRules = new string[] { "Name" };

        List<Car> sortedCars = sortRules
            .SelectMany(sortRule => cars.Where(car => car.Accessories.Any(acc => acc.Name == sortRule)))
            .Distinct().ToList();

        return sortedCars;
    }

我想按排序字段“名称”的值对最终结果进行排序,在本例中为“奥迪”、“宝马”、“马自达”

这一切的工作原理是,用户可以从下拉“名称”中进行选择,结果可以按“名称”的值排序。不幸的是,这就是我无法控制的第三方来源返回数据的方式。

4

2 回答 2

0

要进行排序,Accessory您需要确保该类实现了IComparable接口

public class Accessory : IComparible
{
    public int CompareTo(object obj) {
        //Return 0 for equals
        //Return -1 if this object precedes the given object
        //Return 1 if this object follows the given object
    }
}
于 2013-05-17T22:23:16.173 回答
0

试试这个样本

private static IEnumerable<Car> GetCars(string filterByAccessory, bool sortAscending = true)
{
    var cars = new List<Car>();
    cars.Add(
        new Car()
        {
            Name = "CAR1",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "1" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "0" },
            new Accessory() { Name = "Sat/Nav", Value = "1" },
            new Accessory() { Name = "Side Assist", Value = "0" },
            new Accessory() { Name = "Name", Value = "BMW" },
        }
        });
    cars.Add(
        new Car()
        {
            Name = "CAR2",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "0" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "0" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "0" },
            new Accessory() { Name = "Sat/Nav", Value = "0" },
            new Accessory() { Name = "Side Assist", Value = "0" },
            new Accessory() { Name = "Name", Value = "Mazda" },
        }
        });
    cars.Add(
        new Car()
        {
            Name = "CAR3",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "1" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "1" },
            new Accessory() { Name = "Sat/Nav", Value = "1" },
            new Accessory() { Name = "Side Assist", Value = "1" },
            new Accessory() { Name = "Name", Value = "Audi" },

        }
        });

    var filteredCars = cars.Where(c => c.Accessories.Any(a => a.Name == filterByAccessory));

    var sortedCars = sortAscending
        ? filteredCars.OrderBy(x => x.Accessories.First(a => a.Name == filterByAccessory).Value)
        : filteredCars.OrderByDescending(x => x.Accessories.First(a => a.Name == filterByAccessory).Value);

    return sortedCars;
}
于 2013-05-19T17:26:26.087 回答