Filtering Date

3 posts, 1 answers
  1. inanc
    inanc avatar
    17 posts
    Member since:
    Mar 2009

    Posted 25 Jan 2012 Link to this post

    Hello,
    I am trying to filter a table containing a date column. I prefer to build linq Expression dynamically. So  I am using ;


                          string LinqExpression = string.Empty;
                    Guid userId = MonoSoftware.MonoX.Utilities.SecurityUtility.GetUserId();
                    int BolgeKodu = client.BolgeKoduDon(userId);
                    //string UsrName = User.Identity.ToString();
                    LinqExpression += "BOLGEKODU=" + BolgeKodu.ToString();
                    if (FirmaSecControl.FirmaId != 0)
                    {
                        LinqExpression += " &&  FIRMAID=" + FirmaSecControl.FirmaId;
                    }
                    if (txtTeklifNo.Text.Trim() != String.Empty)
                    {
                        LinqExpression += " &&  TEKLIFNO=\"" + txtTeklifNo.Text.Trim() + "\"";
                    }
                    if (raddtTeklifTar.SelectedDate != null)
                    {
                        LinqExpression += " &&  TEKLIFTAR=\"" + raddtTeklifTar.SelectedDate.ToString().Replace(" 00:00:00","")+ "\"";
                        //LinqExpression += " &&  TEKLIFTAR=" + raddtTeklifTar.SelectedDate.ToString().Replace(" 00:00:00", "") ;
                        //LinqExpression += " &&  TEKLIFTAR=" + raddtTeklifTar.SelectedDate;
                    }
                    if (LinqExpression.Substring(0, 3) == " &&")
                    {
                        LinqExpression = LinqExpression.Remove(0, 3);
                    }

    But I have an error with only date parameters. The error message is:
    Operator '=' incompatible with operand types 'DateTime' and 'String'

    How should I add the date parameter to my query ?
    Thank you.
    Inanc

  2. Answer
    Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 25 Jan 2012 Link to this post

    Hello Inanc,

    do you use the System.Linq.Dynamic from Microsoft? With this framework you would be able to specify the filter like

    using System.Linq.Dynamic;

    var src = Scope.Extent<Person>();
    var q = src.Where("Birthday <= @0", DateTime.Now);
    var r = q.Count();


    Using a parameter is certainly the best way to keep all the values, because string representations have the tendency of loosing information.
    You can obtain the System.Linq.Dynamic from
    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    If you do not (intent to) use MS dynamic linq, you should try DateTime.Parse(stringvalue) .

    All the best,
    Thomas
    the Telerik team

    SP1 for Q3’11 of Telerik OpenAccess ORM is available for download

  3. DevCraft banner
  4. inanc
    inanc avatar
    17 posts
    Member since:
    Mar 2009

    Posted 26 Jan 2012 Link to this post

    It is Ok now.
    Thank you.
Back to Top