不能将lambda表达式转换为“string”类型,因为它不是委托类型 [英] Cannot convert lambda expression to type 'string' because it is not a delegate type

查看:2928
本文介绍了不能将lambda表达式转换为“string”类型,因为它不是委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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; }
}

然后在我的控制器我有

 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?

推荐答案

Include 方法期望一个字符串,而不是一个lambda:

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.

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

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

这篇关于不能将lambda表达式转换为“string”类型,因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆