Linq EF int转换为字符串

xiangaylian 2013-11-29 02:18:06
from a in Tables group a by
new {a.Year,a.Month}
select new Entity
{
Month = a.Year + "-" + a.Month
}

----------------
a.Year和a.Month都为int,Entity.Month为string
上述查询会报错,请问该如何正确实现?
...全文
666 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
小猪八Q 2013-11-29
  • 打赏
  • 举报
回复
引用 4 楼 q107770540 的回复:
我建议你好好看看LINQ的基础,group by写法上有问题:
from a in Tables group a by 
new {a.Year,a.Month} into g
select new Entity
{
   Month = SqlFunctions.StringConvert((double)g.Key.Year) + "-" + SqlFunctions.StringConvert((double)g.Key.Month)
}
claymore1114 2013-11-29
  • 打赏
  • 举报
回复
ToList.....那些非延迟查询可以用了 ,或者如下

 var query=Tables.GroupBy(g=>new{g.Year,g.Month}).Select(s=>new{s.Key.Year,s.Key.Month});
 var result=query.AsEnumerable().Select(s=>new Entity{s.Year+"-"+s.Month});
 result=result.ToList();
xiangaylian 2013-11-29
  • 打赏
  • 举报
回复
引用 9 楼 q107770540 的回复:
using System.Data.Objects.SqlClient; refrence System.Data.Entity.dll; 看看你的EntityFramework.dll是什么版本的
版本是6.0的
q107770540 2013-11-29
  • 打赏
  • 举报
回复
这是MSDN上的原话:
SqlFunctions.StringConvert Method (Nullable<Double>)

This function can only appear within a LINQ to Entities query.

http://msdn.microsoft.com/en-us/library/dd487127(v=vs.110).aspx
q107770540 2013-11-29
  • 打赏
  • 举报
回复
using System.Data.Objects.SqlClient; refrence System.Data.Entity.dll; 看看你的EntityFramework.dll是什么版本的
xiangaylian 2013-11-29
  • 打赏
  • 举报
回复
LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])' method, and this method cannot be translated into a store expression.
xiangaylian 2013-11-29
  • 打赏
  • 举报
回复
引用 4 楼 q107770540 的回复:
我建议你好好看看LINQ的基础,group by写法上有问题: [/code]
嗯,后面的into写掉了。 不过你这个运行也报错了。
bdmh 2013-11-29
  • 打赏
  • 举报
回复
引用 3 楼 xiangaylian 的回复:
[quote=引用 1 楼 bdmh 的回复:] a.Year.ToString() + "-" + a.Month.ToString()
报错了:Linq to Entities does not recognize the method 'System.String ToString()' method,and this method connot be translated into a store expression.[/quote] 不好意思,是我的错,LINQ to Entities中有专用的类SqlFunctions SqlFunctions.StringConvert 返回转换自数值数据的字符数据。
q107770540 2013-11-29
  • 打赏
  • 举报
回复
我建议你好好看看LINQ的基础,group by写法上有问题:
from a in Tables group a by 
new {a.Year,a.Month} into g
select new Entity
{
   Month = SqlFunctions.StringConvert((double)g.Key.Year) + "-" + SqlFunctions.StringConvert((double)g.Key.Month)
}
xiangaylian 2013-11-29
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
a.Year.ToString() + "-" + a.Month.ToString()
报错了:Linq to Entities does not recognize the method 'System.String ToString()' method,and this method connot be translated into a store expression.
Andy__Huang 2013-11-29
  • 打赏
  • 举报
回复
var list=DB.Tables.GroupBy(g=>g.Year.ToString()+'-'+g.Month..ToString()).Select(t=>{return new{YearMonth=t.key};}).ToList();
bdmh 2013-11-29
  • 打赏
  • 举报
回复
a.Year.ToString() + "-" + a.Month.ToString()

8,493

社区成员

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

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