Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqPad Linq Include() extension method is not found even after adding references

I am able to do

var result =  OAS_Questions.Count (oasq => oasq.Id!=0);
result.Dump();

and even

var result =  OAS_Questions;
result.Dump();

But when I try to include child objects of "Questions" say "Opitons" through

var result =  OAS_Questions.Include("OAS_QuestionOptions");
result.Dump();

I am shown the below error

'System.Data.Linq.Table' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Data.Linq.Table' could be found (press F4 to add a using directive or assembly reference)

I have already tried adding references to the below assembly references.

  1. System.Code
  2. System.Data
  3. System.Data.Entity
  4. System.Data.Linq
  5. System.Linq.Expressions
  6. System.Data.DataSetExtensions

But still the extension method "Include()" is not available while composing query and it gives a syntax error.

like image 686
Arvind Singh Avatar asked Feb 23 '13 10:02

Arvind Singh


3 Answers

If you're using EF via LinqPad then a better method is to use the strongly typed version of .Include (http://msdn.microsoft.com/en-us/library/gg671236%28VS.103%29.aspx) as follows:

  1. open your LinqPad Query
  2. right-click -> query properties
  3. Add reference to the EntityFramework.dll
  4. via the Additional namespace import tab add System.Data.Entity

you then have intellisense and can use the strongly typed version of .Include, e.g.:

var result =  OAS_Questions.Include(q => q.OAS_QuestionOptions); 
like image 82
TonyS Avatar answered Sep 24 '22 22:09

TonyS


You need to use a Typed DataContext (ObjectContext or DbContext) from your EntityFramework project's DLL. You can do this by performing the following steps:

  1. Click on the Add Connection link in the top-left corner of the application.
  2. In the Choose Data Context wizard, select the Use a typed data context from your own assembly option.
  3. Click on the LINQPad Driver option appropriate for your solution (LINQ to SQL, EF 4.0 and lower or EF 4.1 and higher), then click Next.
  4. Click Browse in the top-right corner and navigate to then select your EF project assembly.
  5. Select the Entity Data Model, then click OK.
  6. Specify the Server, Log on details and database, then click OK.

You should now be able to use the .Include statement in LinqPad.

like image 23
Ryan Shripat Avatar answered Sep 23 '22 22:09

Ryan Shripat


By default Linqpad uses Linq2Sql DataContext which doesn't let you do an include.

To get the include extension method use a Typed DataContext from your project assembly (EF4.x /EF5)

like image 42
scartag Avatar answered Sep 24 '22 22:09

scartag