2

I I build select and where condition against an entity ObjectSet class using
ObjectQuery.

  ObjectQuery<DbDataRecord> query = context.TestEnt.Select("it.BuySell, "
                        + "it.DepoTerm").Where("Datediff(day,it.RunDateTime,'22-11-2012')=0" );

                var a = query.ToList();
                foreach (var tmp in a)
                {
                    Console.WriteLine(tmp["BuySell"].ToString());
                }

I would like to use datediff function in my where contition , How can I do it ?

1
  • i am not sure,but is not possible in entity framework (possible in Linq). but on question, are you using Linq.Dynamic class too? Commented Dec 11, 2011 at 7:34

2 Answers 2

3

Try this:

var query = context.TestEnt
                   .Where("DiffDays(it.RunDateTime,'22-11-2012')=0" );
                   .Select("it.BuySell, it.DepoTerm")

It is possible that you will have to call CreateDataTime on your string passed data to make it work. Here is list of all supported date functions in ESQL.

You can also use Linq-to-entities:

var query = context.TestEnt
                   .Where(x => SqlFunctions.DateDiff("day", x.RunDateTime,'22-11-2012') == 0)
                   .Select(x => new { x.BuySell, x.DepoTerm }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately "DiffDays(it.RunDateTime,'22-11-2012')=0" ) does not work
0

The ESQL function DiffDays does work, but it's fiddly to get it right. You have to cast the string to a DateTime; however, since the text is converted to SQL using the CreateQuery<> method, you need to cast to System.DateTime or else the query will fail at runtime.

Try this:

ObjectQuery<DbDataRecord> query = context.TestEnt.Select("it.BuySell, it.DepoTerm")
    .Where("DiffDays(it.[RunDateTime],cast('22-11-2012' as System.DateTime))=0");

var a = query.ToList();
foreach (var tmp in a)
{
    Console.WriteLine(tmp["BuySell"].ToString());
}

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.