big issue in converting string to datetime using linq-to-entities

前端 未结 4 977
傲寒
傲寒 2020-11-29 10:38

How can I convert the string to datetime using linq to entities ....

I have got the below query, where the visit_date column datatype is string...

相关标签:
4条回答
  • 2020-11-29 10:49

    I don't think EF supports a translation for a String to DateTime or vice-versa conversion.

    As I see it, you have two options, depending on the format of the date in the string field:

    If the format is fairly simple, a string comparison might be enough:

    // 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
                  };
    

    If the string representation of the date is more complex, and a simple string comparison cannot help, you might consider creating a view on the visits table, which does the conversion for you at database level:

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

    Followed by importing this view into your DataModel. You might need to do some magic to make the relationships work.

    Hope it helps.

    0 讨论(0)
  • 2020-11-29 11:00

    Because DateTime and DateTimeOffset are structs, they are not intrinsically nullable. When you need nullability, there are two ways around this:

    1. Use a Nullable type (i.e., DateTime? or DateTimeOffset?).
    2. Use the static field DateTime.MinValue or DateTimeOffset.MinValue (the default values for these types)
    0 讨论(0)
  • 2020-11-29 11:04

    Try converting the results to a List<> first, and filter the results from the 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           
                  }; 
    
    0 讨论(0)
  • 2020-11-29 11:06

    Convert.ToDatetime is supported by Linq2SQL. The only supported method of Linq to entities are these: http://msdn.microsoft.com/en-us/library/bb738681.aspx

    about your problem ... try to convert startdate and enddate in string and compare the string value in linq expression.

    0 讨论(0)
提交回复
热议问题