var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new
{
MatNR = o.MatNR,
MatDB = o.MatDB,
CreatedOn=o.CreatedOn.ToString(),
CreatedBy = o.CreatedBy,
Id2 = o.MatNR
});

以上语句就会出现这个提示:LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。

解决方法有两种

一、使用SqlFunctions

1、

 CreatedOn = SqlFunctions.DateName("year", o.CreatedOn) + "-"
+ SqlFunctions.DateName("month", o.CreatedOn) +"-"
+ SqlFunctions.DateName("day",o.CreatedOn),

2、

 CreatedOn= SqlFunctions.DateName("yyyy", o.CreatedOn)
+"-"+
SqlFunctions.StringConvert((decimal)SqlFunctions.DatePart("mm", o.CreatedOn))
+"-"+
SqlFunctions.DateName("dd",o.CreatedOn),

3、

CreatedOn = SqlFunctions.DateName("yyyy", o.CreatedOn)
+ "-" +
SqlFunctions.StringConvert((decimal)SqlFunctions.DatePart("mm", o.CreatedOn)).Trim()
+ "-" +
SqlFunctions.DateName("dd", o.CreatedOn),

注意:月份只有1位数!还不够完美。

二、Linq-to-entities 转换到Linq-to-objects

//Do it in two steps - one L2E query that does the L2E supported part, and a L2O query that does the date formatting and other stuff that is best done in .net code...

////Linq-to-entities query to get the fullname, categoryname, and date

//var query = from c in db.Contacts select new { c.FullName, c.Category.CategoryName, c.DateCreated };

////Linq-to-objects query (.AsEnumerable will separate the L2E from L2O part) that call date formatting methods and other stuff that isn't supported by L2E

//var r = from c in query.AsEnumerable() select new { c.FullName, c.CategoryName, ShortDate = c.DateCreated.ToShortDateString() };

var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new
{
MatNR = o.MatNR,
MatDB = o.MatDB,
CreatedOn=o.CreatedOn,
CreatedBy = o.CreatedBy,
Id2 = o.MatNR
}); var a = from c in data.AsEnumerable()
select new
{
MatNR = c.MatNR,
MatDB = c.MatDB,
//CreatedOn = Convert.ToDateTime(c.CreatedOn).ToShortDateString(),// 2014/2/28
//CreatedOn = c.CreatedOn.ToString(),//2014/2/28 15:09:40
CreatedOn =c.CreatedOn.ToString("yyyy-MM-dd"),//如果数据库里的日期字段可以为null,则CreatedOn.ToString("yyyy-MM-dd")这种方式是不可以的,如果不为null则可以;
                        CreatedBy = c.CreatedBy,
Id2 = c.MatNR
};

LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。的更多相关文章

  1. Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)

    1.在工作中碰到这样一个问题: 使用linq时,需要查询两个表,在这两张表中关联字段分别是int,和varchar()也就是string,在linq中对这两个字段进行关联, 如果强制类型转换两个不同类 ...

  2. LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法

    一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...

  3. LINQ to Entities 不识别方法“System.String ToString(System.String)”,因此该方法无法转换为存储表达式。

    来源:https://www.cnblogs.com/hao-1234-1234/p/9112434.html 6  Select的时候,时间无法转换成 年月日  YYMMMdd 报错:LINQ to ...

  4. linq to entity不识别方法"System.String ToString()"

    将班级id以字符串形式输入如:"1111,1112,1113".数据库里的id为int型,在数据路里找到匹配的相应班级转换成列表.在这里爆出问题:不识别方法"System ...

  5. LinQ to entities 不能识别方法“system.string.ToString(system.String)”.因此该方法无法转换为存储表达式

    [我也是刚研究IEnumerable和IQueryable]以下都是个人理解,仅供参考,如有错误欢迎指出~ 在EF里面,使用IQueryable和IEnumerable可以延迟加载. IQueryba ...

  6. LINQ to Entities 不识别方法“System.String ToString(“yyyy-MM-dd”)”

    将Queryable转化为IEnumerable或者直接Tolist()

  7. mvc ef LINQ to Entities 不识别方法“Int32 Parse(System.String)”,因此该方法无法转换为存储表达式。

    private sys_User GetUserInfo() { sys_User model = null; var userId = Convert.ToInt32(AccountHelper.G ...

  8. LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式。

    1.LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式.项目中发现linq to entities 不识别? , ...

  9. LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。

    LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型

随机推荐

  1. java 复习002

    java东西太多了,我都有点小凌乱了,记得太没结构了 java内存回收机制:垃圾收集GC(Garbage Collection) 两种常用方法: 引用计数(早期使用) 简介:堆中对象每次被栈中引用指向 ...

  2. ArcMap10.1无法保存编辑的内容

    问题描述:在arcMap10.1中编辑SDE库中要素,保存编辑内容时报错: 无法保存编辑内容.基础DBMS错误[ORA-29877:failed in the execution of the ODC ...

  3. A题进行时--浙大PAT 1021-1030

    1021: #include<stdio.h> #include<string.h> #include<vector> #include<queue> ...

  4. InputFormat,OutputFormat,InputSplit,RecordRead(一些常见面试题),使用yum安装64位Mysql

    列举出hadoop常用的一些InputFormat InputFormat是用来对我们的输入数据进行格式化的.TextInputFormat是默认的. InputFormat有哪些类型? DBInpu ...

  5. ActiveReport资料

    1. ActiveReports for .NET 2 Online | ActiveReports for .NET 3 Online 2.GroupHeader块 ①GroupHeader块为每个 ...

  6. Apache Hadoop 镜像地址

    HTTP http://apache.fayea.com/ http://apache.opencas.org/ http://mirror.bit.edu.cn/apache/ http://mir ...

  7. SRM 514 DIV1 500pt(DP)

    题目简述 给定一个H×W大小的矩阵,每个格子要么是1~9中的一个数,要么是".",要求你把“.”填成具体的数字(1~9),并且符合以下两个要求: 对于所有的整数r 和 c( 0 & ...

  8. unsupported dynamic reloc R_ARM_REL32 AND hidden symbol '__dso_handle' is not defined

    项目里编译codec src\makefiles\android\codec\Makefileline 25 原本用 4.6 不会报错-L/data/android/android-ndk/sourc ...

  9. Spring properties dependency checking

    In Spring,you can use dependency checking feature to make sure the required properties have been set ...

  10. Java中的Filter过滤器

    Filter简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件 ...