1

I have database on my PC,and into that DB date save in nchar format,i want use this query:

 var query = (from t in behzad.STATUS
              where DateTime.Parse(t.date) >= DateTime.Parse("1394/4/21") <= DateTime.Parse(NOW)
              select p).ToArray();


but I get this error:

Additional information: Method 'System.DateTime Parse(System.String)' has no supported translation to SQL.

How can i solve that problem?

2
  • 1
    Change the database to use proper dates instead of strings. Commented Oct 29, 2015 at 11:23
  • @DavidG i can't change that. Commented Oct 29, 2015 at 11:29

2 Answers 2

2

Use ToList() to come out of SQL domain, and try to parse the date then, something like this:

var query = (from t in behzad.STATUS.ToList()
              where DateTime.Parse(t.date) >= new DateTime(1394,4,21) <= DateTime.Now
              select p).ToArray();

EDIT: Note that this ToList() would fetch all the data from behzad.STATUS entity, and could cause overhead for large result sets.

Sign up to request clarification or add additional context in comments.

4 Comments

It might be but sometimes you can only work with what you've been given
@DavidG He's just asking about parsing of database string, which is known not to work with DateTime.Parse(). The answer reflects only that.
@user3671271 Be warned that if/when this table grows, your application will now grind to a halt because of this.
@DavidG I've already added a note in the original answer to warn him :)
0

You cannot use the parse method in linq to entities and you are using a string value in the db object so cannot parse before and pass in a variable. Try getting your results and then filtering them. This is not ideal with large data sets as you pull a lot more data into memory but is the easiest way to do it:

var data = behzad.STATUS.ToList();
var query = data.Where(t => DateTime.Parse(t.date) >= DateTime.Parse("1394/4/21") <= DateTime.Parse(NOW)).ToArray();

To do it without pulling back all data first your probably best changing the database to store dates instead of string sthen you could do this:

var date1 = DateTime.Parse("1394/4/21");
var date2 = DateTime.Parse(NOW); //If this is the time now then try using DateTime.Now or DateTime.Today
var query = behzad.STATUS.Where(t => t.date/*this is now a dateTime object*/ >= date1 <= date2).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.