I use LINQ-to-SQL in my C# application.
Currently, I copy and paste my Linq-to-SQL queries across the different pages in their Page_Load
method, and I know this is an issue.
I want to create a class that returns a string via a LINQ-to-SQL query, how would I do that? Attempting to reference System.Data.Linq
from a new class in Visual Studio doesn't work.
Should I instead be trying to get these values from the DB through other means? I know I can get LINQ-to-SQL to execute stored procedures and the like.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace myapp.App_Code
{
public class sharedFunctions
{
public string getFirstNameFromUserID(int userID)
{
dbmyappDataContext db = new dbmyappDataContext();
Table<tbLogin> expLogin = db.GetTable<tblLogin>();
var FNQuery = (from FN in tblLogin
where FN.ID == (int)userID
select FN.UserName).FirstOrDefault();
return FNQuery.UserName.ToString();
}
}
}
My main problem with the above, is that when I all the Table
references are underlined by red squiggly lines. When I hover over I get told that
The type or namespace name
Table<tblLogin>
could not be found (are you missing a using directive or an assembley reference?.
If I use this same code in ASPX code behind, it works fine.
Attempting to add using System.Data.Linq
to the top doesn't work either, it doesn't show up in autocomplete. System.Linq is there, though.