无法将 lambda 表达式转换为“字符串”类型;因为它不是委托类型
在我的控制器中,我尝试使用包含 EF4 来选择相关实体,但 lambda 表达式抛出以下错误,
我在实体类中定义了相关实体,例如
public class CustomerSite
{
public int CustomerSiteId { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
然后在我的控制器中我有
var sites = context.CustomerSites.Include(c => c.Customer);
public ViewResult List()
{
var sites = context.CustomerSites.Include(c => c.Customer);
return View(sites.ToList());
}
任何人都可以在我在这里做错了什么的正确方向?
In my controller i am trying to use include with EF4 to select related entities, but the lambda expression is throwing the following error,
i have the related entity defined in the Entity class like
public class CustomerSite
{
public int CustomerSiteId { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
Then in my controller i have
var sites = context.CustomerSites.Include(c => c.Customer);
public ViewResult List()
{
var sites = context.CustomerSites.Include(c => c.Customer);
return View(sites.ToList());
}
Can anyone kindly point me in the right direction on what i'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,这个帖子很旧了,但只是在这里回复以更新它。那么,Entity Framework 4.1 的
Include()
方法具有扩展方法,并且它还接受 lambda 表达式。所以完全有效,您所需要做的就是使用它:
Well, the post is quite old, but just replying here to update it. Well, the
Include()
method with Entity Framework 4.1 has extension methods and it also accepts a lambda expression. Sois perfectly valid, all you need to do is use this:
Include是System.Data.Entity命名空间中的扩展方法,需要添加:
然后就可以使用lambda表达式,而不是字符串。
Include is an extension method in the System.Data.Entity namespace, you need to add:
Then you can use the lambda expression, instead of the string.
包含< /code>
方法需要一个字符串,而不是 lambda:
当然你可以写一个 自定义扩展方法,它可以与 lambda 表达式一起使用并使得您的代码独立于一些神奇的字符串并且重构更友好。
但无论您做什么,请不要将 EF 自动生成的对象传递到您的视图。 使用视图模型。
The
Include
method expects a string, not a lambda:Of course you could write a custom extension method which would work with lambda expressions and make your code independant of some magic strings and refactor friendlier.
But whatever you do PLEASE OH PLEASE don't pass EF autogenerated objects to your views. USE VIEW MODELS.
Include
接受一个字符串,而不是 lambda 表达式。将其更改为
CustomerSites.Include("Customer")
Include
takes a string, not a lambda expression.Change it to
CustomerSites.Include("Customer")
如果您在 Razor 中收到此错误:
例如:
C# 不知道如何将字符串转换为有效的 bool 或已知类型。
因此,按如下方式更改您的字符串:
或
If you are getting this error in Razor:
Ex:
C# doesn't know how to convert the string to valid bool or known type.
So change your string as below:
or