简体   繁体   中英

Entity Framework 6: .Where and .Select that accept string parameters?

In “Programming Entity Framework”, 2nd edition by Julia Lerman, in chapter dedicated to Entity SQL, p. 115, we have the following example of using query builder method to do projection using EF 4.1 .Where and .Select with string parameters:

ObjectQuery<DbDataRecord> contacts = context.Contacts
    .Where("it.FirstName='Robert'")
    .Select("it.Title, it.FirstName, it.LastName");

I am using Entity Framework 6, .Net 4.6, VS 2015. The compiler complains that there are no .Where and .Select that accept string parameters, only lambdas. Is there any solution how to follow this book example?

The example appears to be about the old ObjectQuery API, which shouldn't really be used now. It's still possible to use it with EF6.x though, with something like the following:

ObjectContext objectContext = ((IObjectContextAdapter)conte).ObjectContext;
ObjectSet<DbDataRecord> objectSet = objectContext.CreateObjectSet<DbDataRecord>("DbDataRecords");
// Async version: var res0 = await objectSet.Where("it.FirstName='Robert'").ToListAsync();
var res0 = objectSet.Where("it.FirstName='Robert'").ToList();

That said, you really should use the lambda instead with the new DbContext API.

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