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

  • SEARCH
  • Home
  • 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 6630677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:26:22+00:00 2026-05-25T22:26:22+00:00

I am simply trying to retrieve some data from a database, and display it

  • 0

I am simply trying to retrieve some data from a database, and display it on the _Layouts.cshtml file. And i wanna do this in the with a method in the models folder, right??

My following script down here works, but it’s not pretty, take a look.

Here is my model:

namespace MvcBreakingNews.Models
{
public class ListCategories
    {
        public IList<string> arrTest() {
            IList<string> myList = new List<string>();
            DataClassesDataContext dt = new DataClassesDataContext();

            var q = from c in dt.categories select c;
            foreach (var item in q)
            {
                myList.Add(item.category1.ToString());
            }
                return myList;
            }
        }
    }
}

And here is my _Layout.cshtml

@using MvcBreakingNews.Models
...

@{
   ListCategories objC = new ListCategories();

   foreach (var item in objC.arrTest())
   {
       <li><a href="#">@item</a></li>         
   }
}

Now what i want to do, or what i think i want to do. Is to get rid of the foreach loop in my method and send the array directly to the _Layout.cshtml

How would i do this?

  • 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-05-25T22:26:23+00:00Added an answer on May 25, 2026 at 10:26 pm

    You can use the ToList() extension method:

    public IList<string> arrTest()
    {
        DataClassesDataContext dt = new DataClassesDataContext();
    
        var q = from c in dt.categories
                select c.category1.ToString();
    
        return q.ToList();
    }
    

    Or you could return q directly, which would require changing the return type:

    public IQueryable<string> arrTest()
    {
        DataClassesDataContext dt = new DataClassesDataContext();
    
        return from c in dt.categories
               select c.category1.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to an MSAccess database and retrieve some data. With simple
I am trying to retrieve some rows from the database using simple SELECT statement
I'm trying to extract data from an xml file. A sample of my code
I am simply trying to retrieve an attribute from XML into my Perl program.
Is there a better way than simply trying to open the file? int exists(const
In a WinApp I am simply trying to get the absolute path from a
I've been racking my brains on this for a while, I'm simply trying to
I have some XML data retrieved from a web service that I use to
I have been having some issues retrieving JSON data from a WCF service application
I'm trying to figure out the safest way to retrieve unicode data in 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.