c#-按两个具有不同类型的字段对不同型号的列表进行排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-按两个具有不同类型的字段对不同型号的列表进行排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2248字,纯文字阅读大概需要4分钟。
内容图文
![c#-按两个具有不同类型的字段对不同型号的列表进行排序](/upload/InfoBanner/zyjiaocheng/660/812b516dd0ee40f99ce07f0637cafbff.jpg)
我有两个模型:
public class CarRent
{
public string CarName { get; set; }
public string SystemId { get; set; }
public DateTime RentEndDate { get; set; }
}
public class CarPurchase
{
public string CarName { get; set; }
public string SystemId { get; set; }
public decimal Mileage { get; set; }
}
我需要将它们组合到一个列表中,按CarName分组,然后在每个组中首先需要按SystemId排序模型,但是如果模型具有相同的SystemId-我需要按RentEndDate排序CarRent模型,按Mileage排序CarPurchase.
我尝试过的
我定义了一个接口:
public interface ICarPurchaseOrdered
{
string CarName { get; }
string SystemId { get; }
string Order { get; }
}
并让我的模型来实现它,Order属性仅返回二阶条件的字符串表示形式,然后定义了一个视图模型:
public class GroupedCardList
{
public string CarName { get; set; }
public IEnumerable<ICarPurchaseOrdered> Cars { get; set; }
}
然后我有一个将我的模型分组的石斑鱼:
public class CarGrouper
{
IEnumerable<GroupedCardList> Group(IEnumerable<ICarPurchaseOrdered> cars)
{
return cars.GroupBy(c => c.CarName)
.OrderBy(c => c.Key)
.Select(c => new GroupedCardList()
{
CarName = c.Key,
Cars = c.OrderBy(n => n.SystemId)
.ThenBy(n => n.Order)
});
}
}
但这行不通,因为它会对字符串进行排序,而我在购买Milage = 90的汽车之前先购买Milage = 1200的汽车.
我知道这个例子有些人为,但这完全代表了我现在遇到的问题.请给我一些建议.
解决方法:
一种方法是实现自定义IComparer.如果提取公共基类:
public class Car
{
public string CarName { get; set; }
public string SystemId { get; set; }
}
public class CarRent : Car
{
public DateTime RentEndDate { get; set; }
}
public class CarPurchase : Car
{
public decimal Mileage { get; set; }
}
然后,将IComparer< Car>实现可能如下所示:
public class CarComparer : IComparer<Car>
{
public int Compare(Car x, Car y)
{
// compare by system id first
var order = string.Compare(x.SystemId, y.SystemId);
if (order != 0)
return order;
// try to cast both items as CarRent
var xRent = x as CarRent;
var yRent = y as CarRent;
if (xRent != null && yRent != null)
return DateTime.Compare(xRent.RentEndDate, yRent.RentEndDate);
// try to cast both items as CarPurchase
var xPurc = x as CarPurchase;
var yPurc = y as CarPurchase;
if (xPurc != null && yPurc != null)
return decimal.Compare(xPurc.Mileage, yPurc.Mileage);
// now, this is awkward
return 0;
}
}
然后,您可以将比较器实例传递给List.Sort和Enumerable.OrderBy.
内容总结
以上是互联网集市为您收集整理的c#-按两个具有不同类型的字段对不同型号的列表进行排序全部内容,希望文章能够帮你解决c#-按两个具有不同类型的字段对不同型号的列表进行排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。