日期:2014-05-20 浏览次数:20993 次
已知:
一个等级实体
public class Levels
{
public string ID { get; set; } //编号
public string Name { get; set; } //名称
public int No {get;set;}//排名
}
List<Levels> li = new List<Levels>();
li.Add(new Levels { ID = "A", Name = "一般" });
li.Add(new Levels { ID = "B", Name = "严重" });
li.Add(new Levels { ID = "C", Name = "紧急" });
li.Add(new Levels { ID = "D", Name = "危急" });
一个数据实体
public class Data
{
public string DID { get; set; }//编号
public string LID { get; set; }//等级编号(关联Levels中的ID)
public int Num { get; set; }//次数
}
List<Data> dli = new List<Data>();
dli.Add(new Data { DID = "1", LID = "A", Num = 5 });
dli.Add(new Data { DID = "2", LID = "B", Num = 3 });
dli.Add(new Data { DID = "3", LID = "C", Num = 3 });
int i=1;
var query=from l in li
join d in dli on l.ID equals d.LID into lg
from d in lg.DefaultIfEmpty()
orderby d==null?0:d.Num descending
select new
{
Index=i++,
Name=l.Name,
Num=d==null?0:d.Num
};
class static MyLinqHelper
{
public static IEnumerable<TResult> Select<TInput, TResult>(this IEnumerable<TInput> data, Func<TInput, TResult, int> selector)
{
TResult pre = default(TResult);
int i = 0;
foreach (var item in data)
{
TResult current = selector(item, pre, i);