首页 新闻 会员 周边

EF中SqlFunctions.StringConvert提示该方法无法转换为存储表达式。

0
悬赏园豆:10 [已解决问题] 解决于 2014-07-28 17:41
1 string id = "1";
2                 var Query = from c in db.AdminInfo where SqlFunctions.StringConvert((double)c.Id) == id select c;
3                 return Query.ToList();


报错,提示
LINQ to Entities 不识别方法“System.String StringConvert(System.Nullable`1[System.Double])”,因此该方法无法转换为存储表达式。
这是为什么呢? 求博友指点一下。
EF用的是6.00,数据库是2008R2
命名空间:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data.Entity;
 4 using System.Linq;
 5 using System.Linq.Expressions;
 6 using System.Text;
 7 using SnsDB;
 8 using EntityFramework.Extensions;
 9 using EntityFramework.Reflection;
10 using System.Data.SqlClient;
11 using System.Data.Objects.SqlClient;
12 using System.Data.Objects;


想不通为什么会有问题。

kezhiping的主页 kezhiping | 初学一级 | 园豆:197
提问于:2014-07-28 14:04
< >
分享
最佳答案
0

当EF使用的是6.0版本的时候,添加命名空间
using System.Data.Entity.SqlServer;

public ActionResult Index()
        {
            int Count = 0;
            using (Models.SnsTestProjectDB2 db = new Models.SnsTestProjectDB2())
            {
                string str = "1";
                var aaa = from a in db.AdminInfo where SqlFunctions.StringConvert((double?)a.Id, 1) == str select a;
                var Query = aaa.ToList();
                Count = Query.Count();
            }
            ViewBag.Count = Count;
            return View();
        }

OK,正常运行。

 

本群提供ASP.NET MVC,EF,LINQ,WEB API技术支持,不在乎人多,在乎人精。
ASP.NET MVC群 171560784  
诚邀各路高手、初学者加入。

收获园豆:10
拜蜡笔小新为师 | 菜鸟二级 |园豆:211 | 2014-07-28 17:36
其他回答(4)
0

c.Id 是啥类型?

Launcher | 园豆:45045 (高人七级) | 2014-07-28 14:12
0

就用这个方法,直接.tostring试试吧

吴瑞祥 | 园豆:29449 (高人七级) | 2014-07-28 15:21
0

当EF使用的是6.0版本的时候,添加命名空间
using System.Data.Entity.SqlServer;

public ActionResult Index()
        {
            int Count = 0;
            using (Models.SnsTestProjectDB2 db = new Models.SnsTestProjectDB2())
            {
                string str = "1";
                var aaa = from a in db.AdminInfo where SqlFunctions.StringConvert((double?)a.Id, 1) == str select a;
                var Query = aaa.ToList();
                Count = Query.Count();
            }
            ViewBag.Count = Count;
            return View();
        }

OK,正常运行。

 

本群提供ASP.NET MVC,EF,LINQ,WEB API技术支持,不在乎人多,在乎人精。
ASP.NET MVC群 171560784  
诚邀各路高手、初学者加入。

kezhiping | 园豆:197 (初学一级) | 2014-07-28 17:32
0

还有一种方法是

int Count = 0;
            using (Models.SnsTestProjectDB2 db = new Models.SnsTestProjectDB2())
            {
                string str = "1";
                var aaa = from a in db.AdminInfo where SqlFunctions.StringConvert((double?)a.Id).Trim() == str select a;
                var Query = aaa.ToList();
                Count = Query.Count();
            }
            ViewBag.Count = Count;
            return View();

参考文章:http://blog.csdn.net/xiaojia_boke/article/details/8976760#reply

信息技术的风采 | 园豆:156 (初学一级) | 2016-06-27 05:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册