Tarik
Tarik

Reputation: 81801

Namespace Problem

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

Upvotes: 2

Views: 356

Answers (4)

md1337
md1337

Reputation: 1460

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.

Upvotes: 3

David M
David M

Reputation: 72930

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.

Upvotes: 0

vittore
vittore

Reputation: 17589

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

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

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

Upvotes: 3

Related Questions