Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ladislav Mrnka's advice to use Include

I am trying to use Ladislav Mrnka's advice here to use:

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

    }
}

But I get an error. The compiler doesn't recognize 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

I installed the ADO.NET Entity Framework 4.1 from here.

like image 451
Naor Avatar asked Apr 23 '11 16:04

Naor


1 Answers

Two things:

1) You need a reference (and using) to System.Data.Entity, that is where Include is defined:

using System.Data.Entity;

2) Your class should be marked public and static, otherwise you cant put an extension method in it:

public static class Extensions
{ 

Edit:

You also need to include EntityFramework in your project - if you expand references in your project you should see EntityFramework.

The easiest way to add it is via Nuget:

1.) Open the Package Manager Console (View | Other Windows | Package Manager Console)

2.) Type Install-Package EntityFramework

like image 143
BrokenGlass Avatar answered Oct 11 '22 07:10

BrokenGlass