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
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.