简体   繁体   中英

How to query against non-mapped property in Entity Framework?

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"; }
}

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM