3

I am converting a mapped property's type via encapsulation using the process found here (Convert value when mapping). Like @Mykroft said, this prevents you from writing LINQ queries against the DB.

Using the example properties below, How can I tell Linq to Entities to use the IsActiveBool when writing the statement db.Employee.Where(b => b.IsActive); ?

[Column("IsActive")]
protected string IsActiveBool { get; set; }

[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public bool IsActive
{
    get { return IsActiveBool == "Y"; }
    set { IsActiveBool = value ? "Y" : "N"; }
}
Community
  • 1
  • 1
scottDeveloper
  • 159
  • 2
  • 17

1 Answers1

-2

Using your simplistic example of db.Employee.Where(b => b.IsActive); You could write it like this:

db.Employee.ToList().Where(b => b.IsActive);

Keep in mind that this is going to pull back all rows in the Employee table and then filter on IsActive. If you want SQL server to do the filtering for you (which you should because this isn't a good solution for a table of any real size) then you need to write it as:

db.Employee.Where(b => b.IsActiveBool == "Y");
Wyatt Earp
  • 1,783
  • 13
  • 23
  • This is not really a solution. It shows a bad practice approach to bringing everything into memory and an alternative that works for a narrow example but does not work for the OP's shown example. – Travis J Sep 01 '15 at 19:32
  • @TravisJ Well, maybe I wasn't quite as explicit as I could be that it's not a good solution (I think that's pretty obvious), but that's the reason why I added the second option... – Wyatt Earp Sep 01 '15 at 19:33
  • The second option is hardcoded though, this question is asking for a way to use abstract that logic out. I would assume the real world implementation is more complex then the simple example shown. – Travis J Sep 01 '15 at 19:34
  • @TravisJ, no, you are assuming that's the objective, but it's only in the linked question. I don't see anything in the current question that says `== "Y"` isn't an acceptable solution. – Wyatt Earp Sep 01 '15 at 19:42
  • "How can I tell Linq to Entities to **use the IsActiveBool** when writing the statement **db.Employee.Where(b => b.IsActive);**". I think it clearly states that although `IsActive` is being written in the statement, the underlying use should be `IsActiveBool`. – Travis J Sep 01 '15 at 19:43
  • @TravisJ, You're still assuming the reason for the abstraction. Maybe he just doesn't understand it. In any case, I'm done. – Wyatt Earp Sep 01 '15 at 19:45
  • @WyattEarp and @TravisJ, I don't want to use the string datatype for the boolean expression. I know I could use `IsActiveBool == "Y"` but I don't want to. TravisJ is correct in that my question is how can I use the IsActive boolean to perform the query – scottDeveloper Sep 01 '15 at 21:18