16

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?

5 Answers 5

97

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;
Sign up to request clarification or add additional context in comments.

Comments

13

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.

Comments

9

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.

5 Comments

thanks, but easier said than done, i cant find a solid example of how to use view models to return related data
@Darin Dimitrov Could you please come look at a similar question i have posted. stackoverflow.com/q/16060884/1356321
Could you please give a reference to the update below: stackoverflow.com/a/9428710/1268910
Definitely check out the alternate answer for later versions of Entity Framework, as lambdas are allowed.
Above answer is out-of-date. Correct answer is below. ``` using System.Data.Entity; ```
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.