views:

80

answers:

4
+2  Q: 

Namespace Problem

Hello,

Normally we all do use using System.Linq; and using System.Data.Linq; for example on the code-behind and expect we can reach the members of these namespaces from Source Code like <%= Something.First()%> but when I wrote it, asp.net said it couldn't find First() in the context and I had to add <%@ Import Namespace="System.Linq" which looked very weird to me but it worked out. Since they are targeting at the same class why they both need separate namespace importing.

Code-behind :

using System;
using System.Data.Linq;
using System.Linq;
using System.Text

namespace Something
{
   class Items : System.Web.UI
   {
       //...
   }

}

but also I need to add the same Linq namespace on the Html Source part

<%@Import Namespace="System.Linq"%>

Do I know something wrong or this is some kind of bug in asp.net. I thought when the page is compiling, asp.net combines these two classes and converts html source code into cs class and indicates the control in Control c= new Control(); hierarchy.

Thanks in advance.

P.s : I am trying to reach for example First() in Items.aspx and everything I mentioned about an asp.net page which is Items.aspx

A: 

First is not a method on the class, but an extension method defined in the System.Linq namespace. Even though you may also use this extension method within the code behind, this doesn't mean that the ASP.NET compiler can find the extension method without a hint - hence the <%@ Imports ... %> directive.

Note that the ASP.NET compilation (i.e. of the aspx) is separate from the compilation of the code behind. The latter runs when you build the project; the former runs when you either access the page for the first time, or pre-compile it using "Publish..." or a web deployment project. Hence each compiler needs to be told where to find this extension method if you use it in both places.

David M
Yeah, it is Items.aspx page.
Braveyard
Please explain? What exactly is "Something" then?
David M
Can you please explain the page compilation? I understood that code-behind will be compiled during the compilation time. But you say page will be compiled on first user request but after that it will always stay compiled or when other users request the same page later, will it compiled again? Compiled copies live forever or they are temporary? Thanks.
Braveyard
Here's the MSDN page on the aspnet_compiler: http://msdn.microsoft.com/en-us/library/ms229863%28VS.80%29.aspx. They are not recompiled on every request, no.
David M
+3  A: 

Check your web.config file for a namespaces section and make sure System.Linq is listed there.

Documentation:
http://msdn.microsoft.com/en-us/library/ms164642.aspx

Joel Coehoorn
This avoids having to do it at page level, but doesn't explain the issue to the OP.
David M
So to the reach methods or other namespace members on the source code like spaghetti code, I had to declare the namespaces on the source code as well as code-behind?
Braveyard
Yes, there are two separate compilation steps involved. The code behind compiles when you build your project; the aspx compiles when the page is first accessed, or when you pre-compile your website using "Publish..." or a web deployment project.
David M
+4  A: 

You must specify your namespaces in both places. It's normal behavior. That's needed by the compiler in order to pre-compile the aspx page and the code-behind page separately, before merging them into one class and doing the actual compilation.

By default, a few common namespaces are already included in the aspx page, so you don't need to import them. But in your case you need to import Linq.

EDIT: And as Joel Coehoorn said, you can add to that list of default namespaces in Web.config, should you not want to manually add them in the aspx pages.

md1337
A: 

I would not use First in markup, if you still want to do it , make a wrapper in your code behind , like SomeMethod or SomeProperty and access it from markup as <%=SomeProperty %>

vittore