I may have a fundamental misunderstanding of what is going on here, but I'm having an issue looping through a LinqToSQL class in my razor view:
<h3>Owners</h3> @foreach (var ThisOwner in Prop.PropertyOwnerships.Where(p=p.bIsOwner.Value==true)) { <div class="ODEditEntry"> ...
I'm getting the following error:
Compiler Error Message: CS0012: The type 'System.Data.Linq.EntitySet`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I tried putting @using System.Data.Linq at the top of the cshtml file but it is telling me that Linq doesn't exist in the System.Data namespace. This is obviously not true and, yes, I do have system.data.linq as a reference in my project.
Any Ideas here? Is a import needed? Can I just not do Linq style stuff in my razor views? That would seem....odd?
If so, where you have added a reference to that project it all builds fine, but in the Razor view you are trying to directly access a type from the System. Data. Linq assembly without referencing it in the web project. Try adding the reference to the main web project and see what you get.
To my understanding, System. Linq is generic-level implementation which relies on IEnumerable whereas System. Data. Linq is provider-specific (LINQ to SQL) which relies on IQueryable.
LINQ offers the following advantages: LINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support.
LINQ to SQL is a technology that provides a run-time infrastructure for managing relational data as objects.
You need to import the namespace into your view by adding @using System.Data.Linq
at the top of your view. However if you want it in all your views then you need to add <add namespace="System.Data.Linq" />
to the web.config in your 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.Data.Linq" /> </namespaces> </pages> </system.web.webPages.razor>
Although not relevent to your question you should really try to move this logic out of the view and into the controller, it will make things much easier to debug and means that your presentation is separated from your business logic.
What fixed it for me was to right click the System.Data.Linq assembly reference, then hit properties. In there set Copy Local to true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With