0条评论
还没有人评论过~
我现在要实现Lambda动态添加Select字段,我贴出部分代码
if (Field.ToLower().Contains("cod"))
{
ret.Data = new
{
Total = Total,
Data = GetSelector(data, s => new { s.Id, s.N_StationId, s.N_JckId, s.ReportTime, s.COD_Avg, s.COD_Max, s.COD_Min }).ToList().Select((s, i) => new
{
Num = (Page - 1) * PageSize + 1 + i++,
s.Id,
s.N_StationId,
s.N_JckId,
s.ReportTime,
s.COD_Avg,
s.COD_Max,
s.COD_Min
})
};
}
if (Field.ToLower().Contains("ph"))
{
ret.Data = new
{
Total = Total,
Data = GetSelector(data, s => new { s.Id, s.N_StationId, s.N_JckId, s.ReportTime, s.PH_Avg, s.PH_Max, s.PH_Min }).ToList().Select((s, i) => new
{
Num = (Page - 1) * PageSize + 1 + i++,
s.Id,
s.N_StationId,
s.N_JckId,
s.ReportTime,
s.PH_Avg,
s.PH_Max,
s.PH_Min
})
};
}
private IQueryable<T> GetSelector<T>(IQueryable<T_Report_Year> db, Expression<Func<T_Report_Year, T>> selector)
{
return db.Select(selector);
}
这个代码实现的是只包含其中一个cod或ph时可以动态,那要多个同时包含,怎么改进这段代码呢?
代码生成 Expression 。