2011-04-23 39 views
5

我想使用拉吉斯拉夫Mrnka的建議here使用:拉吉斯拉夫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; 
     } 

    } 
} 

但我得到一個錯誤。編譯器無法識別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實體框架4.1。

回答

6

兩件事情:

1)你需要一個參考(和using)到System.Data.Entity,這正是Include定義:

using System.Data.Entity; 

2)你的類應標publicstatic,否則你不能把它的擴展方法:

public static class Extensions 
{ 

編輯:

您還需要在項目中包含EntityFramework - 如果您在項目中擴展引用,則應該看到EntityFramework

添加它最簡單的方法是通過的NuGet:

1)打開包管理器控制檯(視圖|其他窗口|包 Manager控制檯)

2)型安裝,包裝的EntityFramework

+0

它沒有幫助。我更新了我的問題。 – Naor 2011-04-23 16:30:26

+0

@Naor:您是否將對EntityFramework的引用添加到您的項目中? – BrokenGlass 2011-04-23 16:33:03

+0

我引用System.Data.Entity。它不代表實體框架嗎? – Naor 2011-04-23 16:33:59