Deunz
Deunz

Reputation: 1941

foreach error with EntityCollection<TEntity>

Hello I am getting an error i can't explain it.

I am trying to parse an EntityCollection with a foreach loop

Here is the error I get :

foreach statement cannot operate on variables of type
'System.Data.Objects.DataClasses.EntityCollection<MyEntity>'
because 'System.Data.Objects.DataClasses.EntityCollection<MyEntity>'
does not contain a public definition for 'GetEnumerator'

This is really strange because I had a console application and i was working very well. BUt then I wanted to make a web application.

With the console app, i can perform the foreach, but I get the error in the Web application.

This is driving me crazy. please help :)

Here is the code:

foreach (INITIATIVE_REF_STRATEGIC_AXIS stratAxe in i.INITIATIVE_REF_STRATEGIC_AXIS)
{

}

i.INITIATIVE_REF_STRATEGIC_AXIS is of type System.Data.Objects.DataClasses.EntityCollection<INITIATIVE_REF_STRATEGIC_AXIS>

Upvotes: 1

Views: 2383

Answers (2)

John Nicholas
John Nicholas

Reputation: 4836

This sounds like you are missing an assembly reference.

Your code is giving compile errors and it hasn't yet gone and refreshed all the references properly. Hence why it can;t see stuff that you know is there.

its a bit of a pig ... go delete all your bin directories clean and rebuild all and cross fingers.

failing that start removing references from projects and reading them.

usually fixes eventually.

You probably have another build error in your output ... fixing that will probably let an assembly compile that will then allow something else to compile that will fix your problem here.

If you sort your build errors by the number column and fix the lowest number first you may find things easier.

Upvotes: 3

Fals
Fals

Reputation: 6839

The Linq Extension Methods are not in the context, thats why you are getting this error.

1) Verify if the project has the reference for:

System.Data.Entity.dll

2) If you are using ASP.NET MVC put this at the top of the View

@using System.Linq;

Or in the web.config in the views folder:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Linq" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

Hopes this help you!

Upvotes: 2

Related Questions