我有以下代码:LINQ排序依据是不工作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderByElementSubelement
{
class Program
{
static void Main(string[] args)
{
List<Place> places = new List<Place>();
places.Add(new Place { Address = "Fifth Street", Score = 29, Houses = new List<House> { new House { Owner = "Mike", Score = 32 }, new House { Owner = "Bobby", Score = 3 } } });
places.Add(new Place { Address = "Sixth Street", Score = 29, Houses = new List<House> { new House { Owner = "Mike", Score = 42 }, new House { Owner = "Ted", Score = 45 } } });
places.Add(new Place { Address = "Seventh Street", Score = 29, Houses = new List<House> { new House { Owner = "Randy", Score = 84 }, new House { Owner = "William", Score = 1 } } });
var placesWithMikesHouseOrderedByMikesHouseScore = places.Where(x => x.Houses.Where(y => y.Owner == "Mike").Count() > 0).OrderBy(x => x.Houses.Where(y => y.Owner == "Mike").Select(z => z.Score)).ToList();
}
public class Place
{
public string Address { get; set; }
public int Score { get; set; }
public List<House> Houses { get; set; }
}
public class House
{
public string Owner { get; set; }
public int Score { get; set; }
}
}
}
它抛出该异常:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: At least one object must implement IComparable.
我想查询由麦克的房子的得分在有序与麦克的家的地方地点(你可以从上面看到这个想法)。一个地方有一个房屋清单。 重要提示:您可以假设没有个人所有者可以在每个地方拥有多个房屋!
为什么它希望一个对象在这里实现IComparable,我该如何使这个查询工作?
非常感谢,里德! – Alexandru 2014-08-27 20:46:25