IQueryable does not contain a definition for &#

2020-02-08 05:03发布

I'm trying to use Include extension on IQueryable set, but I have the following issue:

Error 1 'System.Linq.IQueryable<.Model.InsuranceCaseType>' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Linq.IQueryable<.Model.InsuranceCaseType>' could be found (are you missing a using directive or an assembly reference?)

My code:

var allCaseTypes = Uow.InsuranceCaseType.GetAll().Include(a=>a.Geos);

Method GetAll() returns - IQueryable<.Model.InsuranceCaseType>

I have in scope the following namespaces:

using System.Collections;
using System.Collections.Generic;
using System;
using System.Data;
using System.Linq;
using System.Data.Entity;
using System.IO;
using System.Web;
using System.Web.Mvc;

标签: c# linq
6条回答
时光不老,我们不散
2楼-- · 2020-02-08 05:34

If you are using .Net core version then you need to install: Microsoft.EntityFrameworkCore nuget package.

And then:

using Microsoft.EntityFrameworkCore;
查看更多
smile是对你的礼貌
3楼-- · 2020-02-08 05:36

If you're looking for the method Jon is talking about, you'll need to import the following namespace:

using System.Data.Entity.QueryableExtensions

MSDN Link

查看更多
Fickle 薄情
4楼-- · 2020-02-08 05:39

If you deploy directly to your repository, you can use Include(), as per example code below:

public IQueryable<Colaborador> FindAll()
{
     var retRepository = from colaborador in All()
                            .Include(x => x.Cliente)
                            select colaborador;

     return retRepository;
}
查看更多
聊天终结者
5楼-- · 2020-02-08 05:40

Include is not an extension method on Queryable, so it doesn't come together with all the usual LINQ methods. If you are using Entity Framework, you need to import the corresponding namespace:

using System.Data.Entity;
查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-08 05:41

Some further help for others experiencing this issue even after including the using directive. Jon mentioned it but I just want to make it clear as even after reading the answer I was stuck for a while, sorry if it seems obvious but might save someone else some time.

The issue for me was the reference, which was Entity Framework. After using Nuget to install EF the .Include() worked as usual.

This threw me because the same code with the .Include() was working in my main project (MVC app) but wasn't working in a different project in the same solution, even with the using, as it was missing EF. Hope this saves someone else some time.

查看更多
▲ chillily
7楼-- · 2020-02-08 05:48

(I am using EF 7 with ASP.NET 5 Preview, DNX 5.0 Framework)

Adding this solved mine -

using Microsoft.Data.Entity;

Note: It's not System.Data.Entity

查看更多
登录 后发表回答