使用linq-to-entities将字符串转换为datetime的一个大问题

时间:2011-10-12 13:17:53

标签: c# .net winforms entity-framework linq-to-entities

如何使用linq将字符串转换为datetime到实体....

我有以下查询,其中visit_date列数据类型是字符串...

var memberl = from v in abc.visits
              join m in abc.members on v.member_Id equals m.member_Id
              where Convert.ToDateTime(v.visit_Date) >= startdate && 
                    Convert.ToDateTime(v.visit_Date) <= enddate
              group m by new { m.member_Firstname, 
                               m.member_Lastname, m.member_Id } into g
              orderby g.Count()
              select new
              {
                  numVisits = g.Count(),
                  firstname = g.Key.member_Firstname,
                  lastname = g.Key.member_Lastname
              };

不幸的是我无法更改架构......

我收到了错误:

linq to entites does not recognise Convert.ToDatetime method

是否有任何可能的解决方案将字符串转换为Datetime?

更新代码:

根据要求我更新了我的问题

 var data = (from v in abc.visits
                  join m in abc.members on v.member_Id equals m.member_Id
                  select new
                  {
                      MemberID = v.member_Id,
                      VisiteDate = v.visit_Date,
                      FirstName = m.member_Firstname,
                      LastName = m.member_Lastname
                  }).ToList();

      var membersdata = from d in data
                        where Convert.ToDateTime(d.VisiteDate) >= startdate && Convert.ToDateTime(d.VisiteDate) <= enddate
                        group m by new { d.FirstName, d.LastName, d.MemberID } into g
                        orderby g.Count()
                        select new
                        {
                            numVisits = g.Count(),
                            firstname = g.Key.FirstName,
                            lastname = g.Key.LastName
                        }; 

4 个答案:

答案 0 :(得分:17)

我认为EF不支持将String转换为DateTime或反之亦然。

正如我所看到的,您有两种选择,具体取决于字符串字段中日期的格式:

如果格式相当简单,那么字符串比较可能就足够了:

// Convert the boundaries to strings first
// TODO: Set the ToString format option to match the database format
string startDateAsString = startdate.ToString("yyyyMMdd");
string endDateAsString = enddate.ToString("yyyyMMdd");

// Query based on string comparison
var memberl = from v in abc.visits
              join m in abc.members on v.member_Id equals m.member_Id
              where v.visit_Date.CompareTo(startDateAsString) >= 0 && 
                    v.visit_Date.CompareTo(endDateAsString) <= 0
              group m by new { m.member_Firstname, 
                               m.member_Lastname, m.member_Id } into g
              orderby g.Count()
              select new
              {
                  numVisits = g.Count(),
                  firstname = g.Key.member_Firstname,
                  lastname = g.Key.member_Lastname
              };

如果日期的字符串表示形式更复杂,并且简单的字符串比较无济于事,您可以考虑在view表上创建visits,在数据库级别为您进行转换:

CREATE VIEW VisitsWithDate (MemberId, VisitDate)
AS
SELECT MemberId, Convert(datetime, VisitDate, 112) -- For instance
FROM Visits

然后将此视图导入DataModel。你可能需要做一些魔术才能使关系发挥作用。

希望它有所帮助。

答案 1 :(得分:3)

Linq2SQL支持

Convert.ToDatetime。 Linq对实体唯一支持的方法是: http://msdn.microsoft.com/en-us/library/bb738681.aspx

关于您的问题...尝试在字符串中转换startdateenddate并比较linq表达式中的字符串值。

答案 2 :(得分:2)

首先尝试将结果转换为List<>,然后从列表中过滤结果:

var data = (from v in abc.visits 
            join m in abc.members on v.member_Id equals m.member_Id
            select new
            {
                MemberID = v.member_id,
                VisiteDate = v.visit_date,
                FirstName = m.member_FirstName,
                LastName = m.member_LastName
            }).ToList();    


var memberl = from d in data    
              where Convert.ToDateTime(d.VisitDate) >= startdate && Convert.ToDateTime(d.VisitDate) <= enddate           
              group d by new { d.FirstName, d.LastName, d.MemberID } into g           
              orderby g.Count()           
              select new           
              {           
                  numVisits = g.Count(),           
                  firstname = g.Key.FirstName,           
                  lastname = g.Key.LastName           
              }; 

答案 3 :(得分:0)

因为DateTime和DateTimeOffset是结构体,所以它们本质上不可为空。 当您需要可空性时,有两种方法:

  1. 使用Nullable类型(即DateTime?或DateTimeOffset?)。
  2. 使用静态字段DateTime.MinValue或DateTimeOffset.MinValue(默认值 这些类型的值)