Ladislav Mrnka建议使用Include

Nao*_*aor 5 .net c# entity-framework

我想在这里使用Ladislav Mrnka的建议:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个错误.编译器无法识别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
Run Code Online (Sandbox Code Playgroud)

我从这里安装了ADO.NET Entity Framework 4.1 .

Bro*_*ass 6

两件事情:

1)您需要一个引用(和using)System.Data.Entity,即Include定义的位置:

using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)

2)你的类应标publicstatic,否则你不能把一个扩展方法吧:

public static class Extensions
{ 
Run Code Online (Sandbox Code Playgroud)

编辑:

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

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

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

2.)键入Install-Package EntityFramework

  • @Naor:它代表EFv4.EFv4.1是EFv4的附加功能.您必须引用System.Data.Entity.dll和EntityFramework.dll(您将在EFv4.1的安装目录中找到它) (2认同)