简体   繁体   English

实体框架6:.where和.Select接受字符串参数?

[英]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. 在《 Programming Entity Framework》(第二版)中,Julia Lerman着重于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: 115,下面是使用查询生成器方法使用EF .Where.Select以及字符串参数进行投影的示例:

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. 我正在使用Entity Framework 6,.Net 4.6,VS2015。编译器抱怨没有.Where.Select接受字符串参数,只有lambda。 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. 该示例似乎与旧的ObjectQuery API有关,现在不应该真正使用它。 It's still possible to use it with EF6.x though, with something like the following: 不过,仍然可以将其与EF6.x结合使用,如下所示:

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. 就是说,您确实应该将lambda与新的DbContext API结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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