我有以下代碼: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