none
DynamicQueryable datetime RRS feed

  • Question

  • I am using the DynamicQuerable class from microsoft.  I create my where clauses on the fly based on column type  ie string vs a number.  However, I am not sure how to modify this for date.

                    case "int":
    
                        w = String.Format("{0} == {1}", ap.columnName, val);
    
                        break;
    
                    case "datetime":
    
                        w = String.Format("{0} == \"{1}\"", ap.columnName, val);
    
                        break;
    
                    default:
    
                        w = String.Format("{0} == \"{1}\"", ap.columnName, val);
    
                        break;
    
    
    result ------- w = "date == "4/1/1983 12:00:00 AM""

                        var test = (from ph in Context.ProjectHistories.Where(w)
    
                                   select ph);
    
    
    {"Operator '==' incompatible with operand types 'DateTime?' and 'String'"}

    //This following works, but I want to be passing a string like above as this predicate example is not dynamic.



    System.DateTime selDate = new DateTime(1983,4,1);
    var test2 = (from ph in Context.ProjectHistories.Where(e => e.date == selDate) select ph);
    I appreciate all responces.  Thanks Joe.

    joe alt
    Monday, May 11, 2009 2:56 PM

All replies

  • Hi, did you find solution for this problem?
    Friday, February 25, 2011 8:36 PM
  • Hi altja;
    Try it like this:
    // Assuming val is of type DateTime. Use the ToString() of the DateTime to get
    // the correct string format of the date.
    w = String.Format("{0} == DateTime(\"{1}\")", ap.columnName, val.ToShortDateString( ) );
    

    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, March 4, 2011 6:17 PM