Ladislav Mrnka建议使用Include

时间:2011-04-23 16:06:26

标签: 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 个答案:

答案 0 :(得分:6)

两件事:

1)您需要using引用(System.Data.Entity),即定义Include的地方:

using System.Data.Entity;

2)您的课程应标记为publicstatic,否则您无法在其中加入扩展方法:

public static class Extensions
{ 

修改

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

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

1。)打开包管理器控制台(查看|其他Windows |包     经理控制台)

2。)键入 Install-Package EntityFramework