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

前端 未结 5 1643
生来不讨喜
生来不讨喜 2021-02-19 05:32

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 de

相关标签:
5条回答
  • 2021-02-19 05:57

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

    0 讨论(0)
  • 2021-02-19 06:01

    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;
    
    0 讨论(0)
  • 2021-02-19 06:01

    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.

    0 讨论(0)
  • 2021-02-19 06:05

    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.

    0 讨论(0)
  • 2021-02-19 06:07

    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"})    
    
    0 讨论(0)
提交回复
热议问题