-2

I am not sure how I reference the database and the database table through LINQ. I put it as Database.databaseTableName in the code below. I want to return as a JsonResult. I can do this if I were using a class that inherits from DbContext by using ado.net entity model, but I'm not using that.

 public JsonResult getInfo()
    {

        try
        {
            string connectionString = "Data Source = thispartWorks; Initial Catalog = serverName; Persist Security Info = True; User ID = id; Password = pw";
            conn = new SqlConnection(connectionString);
            conn.Open();

            var query = (from bob in Database.databaseTableName
                             orderby bob.ReceivedDate descending
                             select bob).Take(1000);

            return new JsonResult { Data = query, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
        finally
        {
            conn.Close();
        }
    }
  • Try http://stackoverflow.com/questions/17398019/how-to-convert-datatable-to-json-in-c-sharp – A3006 Feb 13 '17 at 13:38
  • what's your question? Does this code not work in some way? If so please describe the problem. – ADyson Feb 13 '17 at 13:38
  • What is not working here? What error you are getting? What's unexpected behavior you are noticing? – Chetan Feb 13 '17 at 13:42
  • http://stackoverflow.com/help/how-to-ask – The Bearded Llama Feb 13 '17 at 13:47
  • @ADyson I don't know how to reference the database table in the linq. I try using databasename.databaseTableName and it's not working. If the name of database was Company and the table name is Salary, then I do Company.Salary. I am new to coding in general, sorry – Iamasking88 Feb 13 '17 at 13:48
  • @ChetanRanpariya I don't know how to reference the database table in the linq. I try using databasename.databaseTableName and it's not working. If the name of database was Company and the table name is Salary, then I do Company.Salary. I am new to coding in general, sorry – Iamasking88 Feb 13 '17 at 13:53
  • So nothing to do with JSON, then. Maybe try a walkthrough. You have to give your code the context of the database connection. https://www.tutorialspoint.com/linq/linq_sql.htm – ADyson Feb 13 '17 at 13:53
  • @ADyson I want to return the linq as a JsonResult – Iamasking88 Feb 13 '17 at 13:57
  • Yes but your stated issue is unrelated to JSON. Your problem is how to construct the linq statement. What you do with it after that is a different issue. – ADyson Feb 13 '17 at 13:57
  • You code shows nothing about implementation of LINQ to SQL. Can you share the implementation of it if you have done it? Forget about JSON, are you able to retrieve the data from database using this code? – Chetan Feb 13 '17 at 14:21

1 Answers1

0

If you don't understand this concept then you might need to do a bit more research on how it all fits together. Consider this article https://weblogs.asp.net/scottgu/using-linq-to-sql-part-1 for a good starting point.

You need to create the local model of your DB for LINQ to interact with.

Trev Davies
  • 391
  • 1
  • 10