Cannot convert lambda expression to type 'string' because it is not a delegate type

半世苍凉 提交于 2019-11-30 18:14:57

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.

amarnath chatterjee

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;

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.

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

Nalan Madheswaran

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"})    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!