This is a migrated thread and some comments may be shown as answers.

Filtering Date

2 Answers 87 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
inanc
Top achievements
Rank 1
inanc asked on 25 Jan 2012, 11:43 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Thomas
Telerik team
answered on 25 Jan 2012, 06:37 PM
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

0
inanc
Top achievements
Rank 1
answered on 26 Jan 2012, 10:49 AM
It is Ok now.
Thank you.
Tags
Development (API, general questions)
Asked by
inanc
Top achievements
Rank 1
Answers by
Thomas
Telerik team
inanc
Top achievements
Rank 1
Share this question
or