0

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.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Lewis Cianci
  • 926
  • 1
  • 13
  • 38
  • 2
    *Attempting to reference System.Data.Linq from a new class in Visual Studio doesn't work.* You are doing something wrong. – Dusan Jun 24 '15 at 12:59
  • 1
    @LewisCianci http://stackoverflow.com/questions/17831623/why-query-object-design-pattern – Christopher Wirt Jun 24 '15 at 13:12
  • And as Christopher Wirt says, your non System.Data.Linq question is a duplicate of this question: http://stackoverflow.com/questions/17831623/why-query-object-design-pattern – George Stocker Jun 24 '15 at 13:19
  • @GeorgeStocker I've edited the original question, is that any better? Also, what does the lack of System.Data.Linq have to do with the Query Object pattern? (this is the first I've heard of something like that) – Lewis Cianci Jun 24 '15 at 13:35
  • @LewisCianci You initially asked two questions; 1) How do I fix the System.data.linq issue, and 2) How do I make my code reusable? Your second question was answered by the post you were linked to; your first question is what you just improved. We ask that you stick to one question in your post, and not pose multiple questions. – George Stocker Jun 24 '15 at 13:38
  • @GeorgeStocker Sorry, its just that to me they are the same thing, as the System.Data.Linq issue was preventing me from making a reusable function (at least, the only way I know how to). That may not be the best way to do it though, as exampled by Christopher's post. Thanks for formatting my original question as well, I always wondered why SO was so pleasant to read! :) – Lewis Cianci Jun 24 '15 at 13:41
  • if you place your cursor on one of the texts with the squigly lines and hold the CTRL key while pressing the "." button do you get any suggestions? i.e CTRL+. ? – Oladipo Olasemo Jun 24 '15 at 14:02

1 Answers1

0

I found a solution that at least resolves the issue around System.Data.Linq not being availible. The class that I had made had the build action set as "Content". Changing this to "Compile" seemed to resolve the issue with not being able to use System.Data.Linq in the class, and the code works now.

Lewis Cianci
  • 926
  • 1
  • 13
  • 38