Single-File ASPX and LINQ

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

<%@ Page Inherits="MyPage" MasterPageFile="~/Master.master" %>
<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>
+3
source share
2 answers

Add

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

It should work on the code.

So the 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>
+12
source

You need to add a namespace LINQ. You are using an ad import.

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

Source: https://habr.com/ru/post/1760429/


All Articles