对30个对象进行优先选择前4个,这个4个根据该对象的Info属性进行判断,它是自定义类型,所以需要用比较器来比较它的大小。
public class CustomType
{
public int? ID { get; set; }
}
public class RecSample
{
public int Name { get; set; }
public int Value { get; set; }
public CustomType Info { get; set; }
public RecSample()
{
Info = new CustomType();
}
}
public class InfoComparer : IComparer<CustomType>
{
public int Compare(CustomType? x, CustomType? y)
{
if (x?.ID == null || y?.ID == null) return 0;
return x.ID.Value.CompareTo(y.ID.Value);
}
}
internal class Program
{
private static void Main(string[] args)
{
Random r = new Random();
List<RecSample> list = new List<RecSample>();
for (int i = 0; i < 30; i++)
{
list.Add(new RecSample
{

1万+

被折叠的 条评论
为什么被折叠?



