Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:47:32+00:00 2026-06-03T06:47:32+00:00

I have this class using linq to sql, how do I implement the same

  • 0

I have this class using linq to sql, how do I implement the same by using normal sql in ASP.NET MVC 3 without use EF?

public ActionResult Index()
{
    var List  = (from c in db.OFFICE
                 join s in db.CAMPUS_UNIVERSITY on c.IdCampus equals s.IdCampus
                 join u in db.UNIVERSITY on s.IdUniversity equals u.IdUniversity
                 select u).ToList();

    return View(List);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:47:33+00:00Added an answer on June 3, 2026 at 6:47 am

    This is just a sample.(Tested & working ).That is y i am keeping the GetUniversities method inside the controller class . I suggest you to move the GetUniversities method to some service layer so that many UI/Controllers can use that.

        public ActionResult Index()
        {
           var items= GetUniversities();
           return View(items);
        }
    
        private List<DataRow> GetUniversities()
        {
            List<DataRow> list=null;
            string srtQry = "SELECT  U.* FROM Office O  INNER JOIN  
                             CampusUniversity CU ON  O.IdCampus equals CU.IdCampus
                             INNER JOIN UNIVERSITY U ON U.IdUniversity=CU.IdUniversity";
            string connString = "Database=yourDB;Server=yourServer;UID=user;PWD=password;";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string strQry = "";
                using(SqlCommand objCommand = new SqlCommand(srtQry, conn))
                {
                   objCommand.CommandType = CommandType.Text;
                   DataTable dt = new DataTable();
                   SqlDataAdapter adp = new SqlDataAdapter(objCommand);
                   conn.Open();
                   adp.Fill(dt);
                   if (dt != null)
                   {
                      list = dt.AsEnumerable().ToList();
                   }
                }
            }
            return list;
        }
    

    Keep in mind that the GetCustomers method returns a List of DataRows. Not your custom domain entities. Entity framework is giving you the list of Domain Entities. So in the custom SQL case, you need to map the Data Row to an instance of your custom object yourself.

    With LINQ, You can convert the List of DataRow to your custom objects like this

    public ActionResult Index()
    {
       var items= GetCustomers();    
    
       var newItems = (from p in items
                           select new
                           {
                               Name= p.Field<String>("Name"),
                               CampusName= p.Field<String>("CampusName")
                           }).ToList();
    
        return View(newItems);
    }
    

    This will give you a list of anonymous type which has 2 properties, Name and CampusName. Assuming Name and CampusName are 2 columns present in the result of your query.

    EDIT2 : As per the Comment, To List these data in a view, Create a view called Index inside your controller( where we wrote this action methods) folder under Views Folder.We need to make it a strongly typed view. But Wait! What type are we going to pass to the view ?

    Our result is annonymous type. So We will create a ViewModel in this case and instead of annonymous, We will return a List of the ViewModel.

    public class UniversityViewModel
    {
      public string UniversityName { set;get;}
      public string CampusName { set;get;}
    }
    

    Now we will update the code in our Index action like this.

    var newItems = (from p in items
                     select new UserViewModel
                     {
                        UniversityName = p.Field<String>("Name"),
                        CampusName = p.Field<String>("CampusName")
                     }).ToList();
    

    The only change is we now mentioned a type here. So the output is no more annonymous type. But known type.

    Let us go back to our View and write code like this.

    @model IEnumerable<SO_MVC.Models.UserViewModel>
    @foreach (var item in Model)
    {
       <p>@item .UniversityName @item.CampusName</p>
    }
    

    This view is strongly typed to a collection of our ViewModel. As usual we are looping thru that and displaying. This should work fine. It is Tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application which is using Linq to SQL classes placed
i have my auto-generated linq to sql classes, and i extend this class using
Here's a little background on my solution: ASP.Net MVC app Using Linq-to-SQL with table-per-hierarchy
I'm using linq to sql for MySql (using DbLinq) in an ASP.NET MVC website.
I have a process in a website (Asp.net 3.5 using Linq-to-Sql for data access)
So we have our J2EE application using Log4j like this public class CustomerController {
I'm using MVC3 and I have a model like this: public class Foo {
i have a mvc web project and i'm using linq to sql i'm using
I have this contact list which I'm building using LINQ to SQL. The query
I'm using Linq to Sql and have a Many-To-Many relation and therefore use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.