无法将 lambda 表达式转换为“字符串”类型;因为它不是委托类型

发布于 2024-11-19 23:31:03 字数 544 浏览 1 评论 0原文

在我的控制器中,我尝试使用包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一花一树开 2024-11-26 23:31:03

嗯,这个帖子很旧了,但只是在这里回复以更新它。那么,Entity Framework 4.1Include() 方法具有扩展方法,并且它还接受 lambda 表达式。所以

context.CustomerSites.Include(c => c.Customer);

完全有效,您所需要做的就是使用它:

using System.Data.Entity;

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. So

context.CustomerSites.Include(c => c.Customer);

is perfectly valid, all you need to do is use this:

using System.Data.Entity;
木格 2024-11-26 23:31:03

Include是System.Data.Entity命名空间中的扩展方法,需要添加:

using System.Data.Entity;

然后就可以使用lambda表达式,而不是字符串。

Include is an extension method in the System.Data.Entity namespace, you need to add:

using System.Data.Entity;

Then you can use the lambda expression, instead of the string.

驱逐舰岛风号 2024-11-26 23:31:03

包含< /code>方法需要一个字符串,而不是 lambda:

public ViewResult List()
{
    var sites = context.CustomerSites.Include("Customer");
    return View(sites.ToList());
}

当然你可以写一个 自定义扩展方法,它可以与 lambda 表达式一起使用并使得您的代码独立于一些神奇的字符串并且重构更友好。

但无论您做什么,请不要将 EF 自动生成的对象传递到您的视图。 使用视图模型

The Include method expects a string, not a lambda:

public ViewResult List()
{
    var sites = context.CustomerSites.Include("Customer");
    return View(sites.ToList());
}

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.

烟燃烟灭 2024-11-26 23:31:03

Include 接受一个字符串,而不是 lambda 表达式。
将其更改为 CustomerSites.Include("Customer")

Include takes a string, not a lambda expression.
Change it to CustomerSites.Include("Customer")

无所的.畏惧 2024-11-26 23:31:03

如果您在 Razor 中收到此错误:

例如:

@Html.RadioButtonFor(model => model.Security, "Fixed", new { @id = "securityFixed"})

C# 不知道如何将字符串转换为有效的 bool 或已知类型。

因此,按如下方式更改您的字符串:

@Html.RadioButtonFor(model => model.Security, "True", new { @id = "securityFixed"}) 

@Html.RadioButtonFor(model => model.Security, "False", new { @id = "securityFixed"})    

If you are getting this error in Razor:

Ex:

@Html.RadioButtonFor(model => model.Security, "Fixed", new { @id = "securityFixed"})

C# doesn't know how to convert the string to valid bool or known type.

So change your string as below:

@Html.RadioButtonFor(model => model.Security, "True", new { @id = "securityFixed"}) 

or

@Html.RadioButtonFor(model => model.Security, "False", new { @id = "securityFixed"})    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文