按年龄范围排序 [英] linq order by age range

查看:87
本文介绍了按年龄范围排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class FullBucket
   {
       public string Type { get; set; }
       public double OpenAmt { get; set; }
       public double SubmitAmt { get; set; }
       public double NoOfClaims { get; set; }
       public double UniquePatient { get; set; }
   }





_result是包含以下值的列表





"_result" is the list which contains values shown below

Type	      OpenAmt	SubmitAmt	NoOfClaims	UniquePatient
31-60 Days     120	    2000	     235	      12
61-90 Days     110	    2000	    1254	      41
00-30 Days	   100	    2000	     248	      74
120+ Days	    90	    2000	     743	      24
91-120 Days	    80	    2000	     832	      41





i想要这样订购:





i want to order it like this:

Type	     OpenAmt	SubmitAmt	NoOfClaims	UniquePatient
00-30 Days	    100	      2000	      248	    74
31-60 Days	    120	      2000	      235	    12
61-90 Days	    110	      2000	      1254	    41
91-120 Days	     80	      2000	      832	    41
120+ Days	     90  	  2000	      743	    24





我试过这样但是因为Type是一个字符串而无法正常工作:(请帮我订购。





I tried like this but its not working since "Type" is a string :( please help me to order it.

List<FullBucket> _df = _result.OrderBy(d => d.Type).ToList();

推荐答案

您可以使用 Dictionary 。请参阅:



You may use Dictionary. See:

Dictionary<string, int> orderOfType = new Dictionary<string, int>();
orderOfType.Add("00-30 Days", 0);
orderOfType.Add("31-60 Days", 1);
orderOfType.Add("61-90 Days", 2);
orderOfType.Add("91-120 Days", 3);
orderOfType.Add("120+ Days", 4);

var _result = _df.OrderBy(x=>orderOfType[x.Type]);


如果你能修改FullBucket类,我会添加一个MinDays(int)属性 - 并更改Linq / SQL以返回它的值。



如果你无法修改它,你能得到一个表达MinDays属性的子类吗?在子类中,MinDays将是一个get-only属性,其get表达式如下所示:

If you are able to modify the FullBucket class, I would add a MinDays (int) property - and change the Linq/SQL to return a value for it.

If you are not able to modify it, can you derive a sub-class that expresses a MinDays property? In the sub-class, MinDays would be a get-only property, whose get expression is something like the following:
public int MinDays
{
   get 
   {
	var minDaysText= (Type == null ? "" : Regex.Match(Type, "^[0-9]+").Value);
	minDaysText = string.IsNullOrEmpty(md) ? "99999" : minDaysText;
	return int.Parse(md);
   }
}



而且,如果你将定期使用MinDays,你应该添加一个可以为空的私有字段(int?),并设置它第一个引用的值...所以你不必对每个引用都进行正则表达式。


And, if you will be using MinDays regularly, you should add a nullable private field (int?), and set its value on the first reference ... so you don't have to do the regex on every reference.


这篇关于按年龄范围排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆