Linq to Entities问题 [英] Linq to Entities Problem

查看:83
本文介绍了Linq to Entities问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此代码中遇到问题

I have problem in this code

using (IERPEntities iERPEntities = new IERPEntities())
        {
            var result = from res in iERPEntities.Item_General
                         where Convert.ToDateTime(res.Date) <= Convert.ToDateTime(stDate)
                              && Convert.ToDateTime(res.Date) >= Convert.ToDateTime(enDate)
                          && res.Name.Contains(txtSelectItemName.Text)
                         select res;
            GridView2.DataSource = result;
            GridView2.DataBind();
        }





这会抛出一个异常,即



this throw an exception which is

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.

推荐答案





长期以来我也面临同样的问题



而不是做在LINQ查询中进行任何类型的转换,首先执行转换并将其分配给任何变量,并在LINQ查询中使用该变量。



所以你的代码需要像这样改变



Hi

Long back I too faced the same problem

Instead of doing the any kind of casting inside your LINQ Query, do the casting first and assign it into any variable and use that variable inside the LINQ Query.

So your code need to be altered like this

DateTime dtStart = Convert.ToDateTime(stDate);
        DateTime dtEnd = Convert.ToDateTime(enDate);

        using (IERPEntities iERPEntities = new IERPEntities())
        {
            var result = from res in iERPEntities.Item_General
                        where res.Date <= dtStart
                        && res.Date >= dtEnd
                        && res.Name.Contains(txtSelectItemName.Text)
                        select res;
            GridView2.DataSource = result;
            GridView2.DataBind();
        }





我希望它会让你对这个问题有所了解。



I hope it will give you some idea regarding the problem.


这篇关于Linq to Entities问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆