Linq结果“状态”字段使用枚举的Name替换怎么实现

一落山川_ 2020-09-15 12:23:44
var query = (from p in dc.ProgramHead
join u in dc.USER on p.Requestor equals u.USERID
where p.PaymentMethod == "Product"
&& (new int?[] { 3, 7, 8, 9, 10, 11, 12 }).Contains(p.State)
&& p.FunctionId != null
orderby p.PGM_NO descending
select new ProgramHeadReport()
{
OralceOrder = p.OracleOrderNumber,
PGM_NO = p.PGM_NO,
Requestor = u.FIRSTNAME + " " + u.LASTNAME,
ShipmentDate = p.ShipmentDate,
State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State)
}).ToList();


这种写法编译不报错,运行报错:
LINQ to Entities does not recognize the method 'System.String GetName(System.........

不用这种方案,怎么实现这个比较好啊,求大神指点!!!







...全文
2595 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
一落山川_ 2020-09-15
  • 打赏
  • 举报
回复
State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State)

正怒月神 2020-09-15
  • 打赏
  • 举报
回复
引用 5 楼 Kevin Zhou@ 的回复:
[quote=引用 3 楼 正怒月神 的回复:]老生常谈的问题。因为数据之前还是 IQueryable<> 改成如下:
var query = (from p in dc.ProgramHead
                             join u in dc.USER on p.Requestor equals u.USERID
                             where p.PaymentMethod == "Product"
                             && (new int?[] { 3, 7, 8, 9, 10, 11, 12 }).Contains(p.State)
                             && p.FunctionId != null
                             orderby p.PGM_NO descending).ToList()
                             .select new ProgramHeadReport()
                             {
                                 OralceOrder = p.OracleOrderNumber,
                                 PGM_NO = p.PGM_NO,
                                 Requestor = u.FIRSTNAME + " " + u.LASTNAME,
                                 ShipmentDate = p.ShipmentDate,
                          State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State)
                             }.ToList();
emmm.....你贴的这个代码不能用,tolist()之后descending后提示必须以select结尾,后面的new也标红[/quote] 你稍微改一下。。。。
var query = (from p in dc.ProgramHead
                             join u in dc.USER on p.Requestor equals u.USERID
                             where p.PaymentMethod == "Product"
                             && (new int?[] { 3, 7, 8, 9, 10, 11, 12 }).Contains(p.State)
                             && p.FunctionId != null
                             orderby p.PGM_NO descending).ToList()
                             .select new 
                             {
                                 p.OracleOrderNumber,
                                 p.PGM_NO,
                                 u.FIRSTNAME + " " + u.LASTNAME,
                                 p.ShipmentDate,
                                p.State)
                             }.ToList()
.select new ProgramHeadReport()(x=>
OralceOrder = p.OracleOrderNumber,
                                 PGM_NO = p.PGM_NO,
                                 Requestor = u.FIRSTNAME + " " + u.LASTNAME,
                                 ShipmentDate = p.ShipmentDate,
                          State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State) );
一落山川_ 2020-09-15
  • 打赏
  • 举报
回复
引用 3 楼 正怒月神 的回复:
老生常谈的问题。因为数据之前还是 IQueryable<>

改成如下:
var query = (from p in dc.ProgramHead
join u in dc.USER on p.Requestor equals u.USERID
where p.PaymentMethod == "Product"
&& (new int?[] { 3, 7, 8, 9, 10, 11, 12 }).Contains(p.State)
&& p.FunctionId != null
orderby p.PGM_NO descending).ToList()
.select new ProgramHeadReport()
{
OralceOrder = p.OracleOrderNumber,
PGM_NO = p.PGM_NO,
Requestor = u.FIRSTNAME + " " + u.LASTNAME,
ShipmentDate = p.ShipmentDate,
State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State)
}.ToList();



emmm.....你贴的这个代码不能用,tolist()之后descending后提示必须以select结尾,后面的new也标红
正怒月神 2020-09-15
  • 打赏
  • 举报
回复
orderby p.PGM_NO descending).ToList() 这里先 tolist()。把数据加载到本地
正怒月神 2020-09-15
  • 打赏
  • 举报
回复
老生常谈的问题。因为数据之前还是 IQueryable<> 改成如下:
var query = (from p in dc.ProgramHead
                             join u in dc.USER on p.Requestor equals u.USERID
                             where p.PaymentMethod == "Product"
                             && (new int?[] { 3, 7, 8, 9, 10, 11, 12 }).Contains(p.State)
                             && p.FunctionId != null
                             orderby p.PGM_NO descending).ToList()
                             .select new ProgramHeadReport()
                             {
                                 OralceOrder = p.OracleOrderNumber,
                                 PGM_NO = p.PGM_NO,
                                 Requestor = u.FIRSTNAME + " " + u.LASTNAME,
                                 ShipmentDate = p.ShipmentDate,
                          State = Enum.GetName(typeof(ADFSystem.BasicItem.Constants.Program_State), p.State)
                             }.ToList();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧