开发者

Single-file ASPX and LINQ

开发者 https://www.devze.com 2023-01-12 17:53 出处:网络
there\'s a CMS system and there\'s aspx page without backend file. I can add server code straight to the .aspx wrapped with <script language=\"C#\" runat=\"server\"> tag. But compiler generates

there's a CMS system and there's aspx page without backend file. I can add server code straight to the .aspx wrapped with <script language="C#" runat="server"> tag. But compiler generates an error because I use LINQ in my code and I don't have using System.Linq; statement anywhere. And I can't add using inside .aspx file (error again). What should I do?

<%@ Page Inherits="MyPage" MasterPageFile="~/Master.master" %>
<script language="C#" runat="server">
[System.Web.Services.WebMethod]
publi开发者_如何学Pythonc static List<string> GetA()
{
    MyDataContext db = new MyDataContext();

    var result = from a in db.A
                 select a;

    return result.ToList();

}
</script>


Add

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

Above the code should work.

So final code should look like

<%@ Page Inherits="MyPage" MasterPageFile="~/Master.master" %>
<%@ Import Namespace = "System.Linq" %>
<script language="C#" runat="server">
[System.Web.Services.WebMethod]
public static List<string> GetA()
{
    MyDataContext db = new MyDataContext();

    var result = from a in db.A
                 select a;

    return result.ToList();

}
</script>


You need to add the LINQ namespace. You use the import declaration.

<%@ Page Inherits="MyPage" MasterPageFile="~/Master.master" %>
<%@ Import Namespace="System.Data.Linq" %>
<script language="C#" runat="server">
...
0

精彩评论

暂无评论...
验证码 换一张
取 消