请考虑以下代码:
var Final = result.OrderBy(p => p.AreaCode).ThenBy(p => p.PCode).Skip(PageSize * (PageNo - 1)).Take(PageSize);
if (PageSize == 0)
{
Final = result.OrderBy(p => p.AreaCode).ThenBy(p => p.PCode);
}
因为匿名类型应该分配给表达式,所以我不能这样写:
var Final ;
if(PageSize > 0 )
{
Final = result.OrderBy(p => p.AreaCode).ThenBy(p => p.PCode).Skip(PageSize * (PageNo - 1)).Take(PageSize);
}
else
{
Final = result.OrderBy(p => p.AreaCode).ThenBy(p => p.PCode);
}
如何更好地编写丑陋的代码?
感谢
答案 0 :(得分:2)
你可以像下面这样做得更好:
var Final = result.OrderBy(p => p.AreaCode).ThenBy(p => p.PCode);
if (PageSize > 0)
Final = Final.Skip(PageSize * (PageNo - 1)).Take(PageSize);