LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法 [英] LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method

查看:484
本文介绍了LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq的新手,并且以下查询不断返回无法识别System.DateTime"错误.我已经尝试了解析和转换,但都没有用.这是我的查询:

I'm a newbie to Linq and the below query keeps returning "does not recognize System.DateTime" error. I've tried Parse and Convert and neither works. Here's my query:

mrcEntities context = GetContext();

mrcEntities context = GetContext();

        var query = from c in context.tblClients
                    where (c.FirstName != null || c.LastName != null)
                       && c.EligibilityDate >= DateTime.Parse("10/01/2011")
                       && c.EligibilityDate <= DateTime.Parse("04/30/2012")
                      orderby c.ClientID
                    select new
                    {
                        ClientID = c.ClientID,
                        FirstName = c.FirstName,
                        LastName = c.LastName,
                        MiddleName = c.MidName,
                        SSN = c.SSN,
                        DOB = c.DOB,
                        Sex = c.Gender,
                        Ethnic = c.EthnicCode
                    };

        clientRowCnt = query.Count();

任何帮助将不胜感激.

推荐答案

这是因为EF无法将DateTime.Parse转换为商店中可用的函数.如果您替换对DateTime.Parse()的调用结果,并在查询中使用这些变量,它应该可以正常工作.

It's because EF can't turn DateTime.Parse into a function available on the store. If you replace the results of the calls to DateTime.Parse() and use those variables in your query it should work fine.

var from = DateTime.Parse("10/01/2011");
var to = DateTime.Parse("04/30/2012");
var query = from c in context.tblClients
                    where (c.FirstName != null || c.LastName != null)
                       && c.EligibilityDate >= from
                       && c.EligibilityDate <= to
                      orderby c.ClientID
                    select new
                    {
                        ClientID = c.ClientID,
                        FirstName = c.FirstName,
                        LastName = c.LastName,
                        MiddleName = c.MidName,
                        SSN = c.SSN,
                        DOB = c.DOB,
                        Sex = c.Gender,
                        Ethnic = c.EthnicCode
                    };

        clientRowCnt = query.Count();

这篇关于LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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