c# - Ladislav Mrnka 关于使用 Include 的建议

标签 c# .net entity-framework

我正在尝试使用 Ladislav Mrnka 的建议 here使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Entity;

namespace SimTask.Data.EF4
{
    public static class Extensions
    {
        public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query,
            params Expression<Func<T, object>>[] includes)
            where T : class
        {
            if (includes != null)
            {
                query = includes.Aggregate(query,
                          (current, include) => current.Include(include));
            }

            return query;
        }

    }
}

但是我得到一个错误。编译器无法识别 current.Include:

Error   7   'System.Linq.IQueryable<T>' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Linq.IQueryable<T>' could be found (are you missing a using directive or an assembly reference?)   C:\MySolution\MyProj.Data.EF4\Extensions.cs 19  57  MyProj.Data.EF4

我从 here 安装了 ADO.NET Entity Framework 4.1 .

最佳答案

两件事:

1) 您需要对 System.Data.Entity 的引用(和使用),这是定义 Include 的地方:

using System.Data.Entity;

2) 你的类应该被标记为publicstatic,否则你不能在里面放一个扩展方法:

public static class Extensions
{ 

编辑:

您还需要在项目中包含 EntityFramework - 如果您在项目中展开引用,您应该会看到 EntityFramework

添加它的最简单方法是通过 Nuget:

1.) 打开包管理器控制台(查看 | 其他窗口 | 包 管理器控制台)

2.) 输入Install-Package EntityFramework

关于c# - Ladislav Mrnka 关于使用 Include 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5765291/

相关文章:

c# - 发送和接收文件socket编程

c# - GetHashCode() 与 ^

c# - 在 C# 中使用静态方法实现接口(interface)

c# - WCF 单例服务错误 : The service type provided could not be loaded as a service because it does not have a default constructor

entity-framework - Breeze 中的多对多关系

c# - Html.DropDownList - 如何添加额外的 <option> 到列表

c# - 如何删除文本框上的小写字母?

nhibernate - Entity Framework 2 和 NHibernate 相比如何?

c# - 使用 Fluent API 级联删除

c# - 如何在保存数组的类之外引用数组元素?