4

My main concern is changing the property value before expression evaluation "not datetime parsing logic" as it's just one case in other case i need to change other property value from x to y

In my application with EntityFramework 6 i have application setting for datetime input format "mm/dd/yyyy" or "dd/mm/yyyy", when a user make a request (querying or saving changes) i want to change all datetime format to mm/dd/yyyy, if setting was mm/dd/yyyy no changes needed, but if setting was dd/mm/yyyy then i need to convert it to mm/dd/yyyy, i need to know where and how to override the property value and achieve the goal, i'm novice in ExpressionVisitors.

This could be the starting point

internal class DateTimeInterceptor : IDbCommandTreeInterceptor
{
    public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
    {
        if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
        {
            var queryCommand = interceptionContext.Result as DbQueryCommandTree;
            if (queryCommand != null)
            {
                var newQuery = queryCommand.Query.Accept(new DateTimeQueryVisitor());
                interceptionContext.Result = new DbQueryCommandTree(queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery);
            }
        }
    }
}

internal class DateTimeQueryVisitor : DefaultExpressionVisitor
{
    public override DbExpression Visit(DbScanExpression expression)
    {
        if (!expression.Target.ElementType.MetadataProperties.Any(mp => mp.Name.GetType() == typeof(DateTime)))
        {
            return base.Visit(expression);
        }

        // here i should do the work for changing the property value
        // Change the expression

        var binding = expression.Bind();
        return binding.Expression; //should be replaced with the modified expression
    }
}

Thank you

Abou-Emish
  • 2,201
  • 1
  • 22
  • 22
  • post some code so it will be easy to find out. – Frebin Francis Feb 21 '15 at 16:05
  • 1
    What is "proper format"? – Ashley Medway Feb 21 '15 at 16:10
  • 1
    A `DateTime` doesn't have _any_ implicit format. Sounds like you save your `DateTime` values as a character in your database. That's a bad idea. – Soner Gönül Feb 21 '15 at 16:14
  • nevermind about the format of datetime or the conversion :), i just need the EntityFrameWork part where i can detect the property of type DateTime and change it's value, for example working with ExpressionVisitors, ParameterVisitor or something like that – Abou-Emish Feb 21 '15 at 18:06
  • i updated the question with code – Abou-Emish Feb 21 '15 at 18:19
  • It's still quite unclear what you're asking. Could you give an example of what you are *actually* trying to achieve? As-is, this smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). (You've identified expression visitors to solve some problem, and are asking about expression visitors rather than explaining the problem thoroughly.) – Matt Johnson-Pint Feb 21 '15 at 18:37
  • i have application setting for datetime input format "mm/dd/yyyy" or "dd/mm/yyyy", when a user make a request (querying or saving changes) i want to change all datetime format to mm/dd/yyyy, if setting was mm/dd/yyyy no changes needed, but if setting was dd/mm/yyyy then i need to convert it to mm/dd/yyyy – Abou-Emish Feb 21 '15 at 18:47
  • What is the *type* of the expression property you want to change? – Lasse V. Karlsen Feb 21 '15 at 19:42
  • DateTime type, i want to catch the property value and do some logic then replace modify the expression – Abou-Emish Feb 21 '15 at 19:52
  • I'm completely lost on how this has anything to do with Entity Framework. A query in EF would be working against a native type, such as `DateTime` - which *has no format*. A `DateTime` only works with formats when converting to or from *strings*. It's nonsensical to say you want to change datetime formats in an EF query. – Matt Johnson-Pint Feb 23 '15 at 00:24

2 Answers2

0

I think its undoable, as a human and not a machine tell me, what format is this: 02/02/2014? can you guess which is the day and which is the month here? anyway about DateTime parsing you can read some here:

EDIT sorry I meant to comment and not answer, if anyone got a problem with this as an answer please do tell I'll remove it.

Community
  • 1
  • 1
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
  • never mind about the datetime parsing, could you tell me how to change expression property value before executing the expression in EntityFramework – Abou-Emish Feb 21 '15 at 19:05
0

You can't recognize the formatting of the datetime which user is using that. You should know that the datetime is same as the system's datetime which you set that in your machine's control panel.

The only way that you may get user is using another format is in the exception's of days and months.

For example you want to have mm/dd/yyyyso if user used dd/mm/yyyy format so it will cause an error after number 12. Then you can change to your special datetime.

Imagine your system set to mm/dd/yyyy then you can use these codes :

        try
        {
            DateTime dt = DateTime.Parse(Console.ReadLine());
            int month = dt.Month;
            string datetime = dt.ToShortDateString();
            // Do Whatever You Want With The String
            .
            .
            .
            .

        }
        catch (Exception)
        {
            Console.WriteLine("EORROR. Incorrect DateTime Format ...");
            // Now You Must Change dd/mm/yyyy to mm/dd/yyyy
            .
            .
            .
            .
        }

NOTE : It won't give you any error if your system's date set to dd/mm/yyyy format. Make sure you set your system's format to mm/dd/yyyy for working the code.

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
  • thanks for the datetime parsing part, but if you read my question again you will understand that i care more about EntityFrameWork expression property value change – Abou-Emish Feb 21 '15 at 19:47
  • @MohamedEmaish You can't recognize the format of that until it uses the wrong format of datetime. You can simply give a text to user that ask them to use mm/dd/yyyy format. – Ali Vojdanian Feb 21 '15 at 19:52