LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method

前端 未结 2 875
-上瘾入骨i
-上瘾入骨i 2021-01-25 09:51

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:

2条回答
  •  囚心锁ツ
    2021-01-25 10:37

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

提交回复
热议问题