不能将lambda表达式转换为类型“string”,因为它不是一个委托类型。

时间:2022-06-18 16:33:31

In my controller i am trying to use include with EF4 to select related entities, but the lambda expression is throwing the following error,

在我的控制器中,我试图使用包含和EF4来选择相关的实体,但是lambda表达式抛出如下错误,

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?

我在这里做错了什么,谁能指点我正确的方向吗?

5 个解决方案

#1


9  

The Include method expects a string, not a lambda:

Include方法需要一个字符串,而不是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.

当然,您可以编写一个自定义扩展方法,该方法可以与lambda表达式一起工作,并使您的代码独立于一些神奇的字符串,并重构友好性。

But whatever you do PLEASE OH PLEASE don't pass EF autogenerated objects to your views. USE VIEW MODELS.

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

#2


71  

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

这篇文章很老了,但只是在这里回复更新。包含实体框架4.1的Include()方法有扩展方法,它也接受lambda表达式。所以

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

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

是完全有效的,你所需要做的就是:

using System.Data.Entity;

#3


8  

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

Include是System.Data中的扩展方法。实体名称空间,需要添加:

using System.Data.Entity;

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

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

#4


1  

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

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

#5


0  

If you are getting this error in Razor:

如果你在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.

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

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

#1


9  

The Include method expects a string, not a lambda:

Include方法需要一个字符串,而不是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.

当然,您可以编写一个自定义扩展方法,该方法可以与lambda表达式一起工作,并使您的代码独立于一些神奇的字符串,并重构友好性。

But whatever you do PLEASE OH PLEASE don't pass EF autogenerated objects to your views. USE VIEW MODELS.

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

#2


71  

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

这篇文章很老了,但只是在这里回复更新。包含实体框架4.1的Include()方法有扩展方法,它也接受lambda表达式。所以

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

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

是完全有效的,你所需要做的就是:

using System.Data.Entity;

#3


8  

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

Include是System.Data中的扩展方法。实体名称空间,需要添加:

using System.Data.Entity;

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

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

#4


1  

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

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

#5


0  

If you are getting this error in Razor:

如果你在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.

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

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