1

I have an Expression

DateTime date = DateTime.Now;
Expression<Func<MyClass, bool>> exp = null;

my goal should be something like this

exp = x => x.Year > date.Year

but unfortunately MyClass.Year field is a string and I need to parse it. I have problems with Convert.ToInt32(x.Year) at the time EF converts the value to sql. How can I translate int.Parse operation into a correct sql statement with EF? Is there a way to do something like this?

exp = x => 
{
  int val = int.Parse(x.Year);
  return val > date.Year
}

There is a work around to do it just to compile it properly, but still looking for the other way, EF can't translate those call into sql:

private bool GetExpression(MyClass myClass, DateTime endDate)
{
    int year = 0;
    var res = int.TryParse(myClass.Year, out year);

    if (res)
        return year > endDate.Year;

    return false;
}

exp = x => GetExpression(x, DateTime.Now)
svick
  • 236,525
  • 50
  • 385
  • 514
Zinov
  • 3,817
  • 5
  • 36
  • 70
  • How is that different from `x => int.Parse(x.Year) > date.Year`? And have you tried `(int)x.Year`? Also, not to play the devil's advocate, but years should order just fine as strings, at least until we get to year 10 000. – Luaan Sep 26 '16 at 15:45
  • `SqlFunctions::StringConvert` – Wiktor Zychla Sep 26 '16 at 15:46
  • I tried both parts, I put year just to name the field, can be Amount. The thing is when I am trying to use something like this is failing exp = x => Convert.ToInt32(x.Year) >= startDate.Year. And then I had the doubt of how to do it inside a linq statement – Zinov Sep 26 '16 at 15:48
  • 1
    Given that you have a `Year` field, you really should just change that DB column to be numeric, rather than a string. You don't want to have to be parsing it every time you use it. – Servy Sep 26 '16 at 15:52
  • 2
    You can't change all the DBs that you get if you see this problem, that is not the answer, sometimes you deal with legacy db and that can have a big impact. – Zinov Sep 26 '16 at 15:54
  • Possible duplicate of [Entity Framework/Linq EXpression converting from string to int](http://stackoverflow.com/questions/16694716/entity-framework-linq-expression-converting-from-string-to-int) – Zinov Sep 26 '16 at 18:24

1 Answers1

-1

You can change your variable to string and use string.Compare like this:

DateTime date = DateTime.Now;
string dateString = date.ToString();

Expression<Func<MyClass, bool>> exp = 
   x => string.Compare(x.Year, dateString) > 0;

This will return the same results as long as your Years are all 4 characters

Aducci
  • 26,101
  • 8
  • 63
  • 67
  • that makes the comparison to be based on string comparison not on numerical comparison – Zinov Sep 26 '16 at 18:00
  • @Zinov - is that an issue? If so, post an example of your data where the comparison fails. There might be another way around it. – Aducci Sep 26 '16 at 18:02
  • @Aducci 1000 is less than 5, if you compare them as strings, rather than numbers. – Servy Sep 26 '16 at 18:13
  • I found a couple of comments on this post, this is feature that was requested for code first http://stackoverflow.com/questions/16694716/entity-framework-linq-expression-converting-from-string-to-int/24027242#24027242 – Zinov Sep 26 '16 at 18:22
  • @Servy - Ok, but does the OP's dataset contain years that are less than `1000` or bigger than `9999`? – Aducci Sep 26 '16 at 18:45