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();