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 7867765
In Process

The Archive Base Latest Questions

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

Say i have a List with items -17 Screen – 100GB HD -10788 Firewire

  • 0

Say i have a List with items

-17″ Screen
– 100GB HD
-10788 Firewire
-Lock Cable
-Monitor
-Mouse
-Keyboard
– USB

I want to iterate a list for items starting from A to c, D to F and so on…

I want this to run for items starting from A to items starting C
   @foreach (var item in Model.Items.OrderBy(i => i.Title))
            {
// Code
}

I want this to run for items starting from D to items starting F
   @foreach (var item in Model.Items.OrderBy(i => i.Title))
         {
// Code
}

Any Help ?

  • 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-03T00:47:45+00:00Added an answer on June 3, 2026 at 12:47 am

    I would start by defining a view model (as always):

    public class MyViewModel
    {
        public string LetterRange { get; set; }
        public string[] Titles { get; set; }
    }
    

    then a controller action that will convert fetch the model from somewhere and then map it to a view model.

    Remark: in this example I will put the mapping code between the model and the view model inside the controller action but normally this should go into a separate mapping layer. For example if you use AutoMapper that could be a great place.

    So:

    public ActionResult Index()
    {
        // The model could be of any form and come from anywhere but
        // the important thing is that at the end of the day you will have
        // a list of titles here
        var model = new[] 
        { 
            "17\" Screen", 
            "100GB HD", 
            "10788 Firewire", 
            "Lock Cable", 
            "Monitor",
            "Mouse", 
            "Keyboard",
            "USB" 
        };
    
        // Now let's map this domain model into a view model 
        // that will be adapted to the requirements of our view.
        // And the requirements of this view is to group the titles
        // in ranges of 3 letters of the alphabet
        var viewModel = Enumerable
            .Range(65, 26)
            .Select((letter, index) => new 
            { 
                Letter = ((char)letter).ToString(), 
                Index = index 
            })
            .GroupBy(g => g.Index / 3)
            .Select(g => g.Select(x => x.Letter).ToArray())
            .Select(range => new MyViewModel
            {
                LetterRange = string.Format("{0}-{1}", range.First(), range.Last()),
                Titles = model
                    .Where(item => item.Length > 0 && range.Contains(item.Substring(0, 1)))
                    .ToArray()
            })
            .ToArray();
    
        // Let's add those titles that weren't starting with an alphabet letter
        var other = new MyViewModel
        {
            LetterRange = "Other",
            Titles = model.Where(item => !viewModel.Any(x => x.Titles.Contains(item))).ToArray()
        };
    
        // and merge them into the final view model
        viewModel = new[] { other }.Concat(viewModel).ToArray();
    
        return View(viewModel);
    }
    

    and now all that’s left in the corresponding view is to display the titles according to the requirements:

    @model MyViewModel[]
    
    @foreach (var item in Model)
    {
        <h2>@item.LetterRange</h2>
        foreach (var title in item.Titles)
        {
            <div>@title</div>
        }
    }
    

    and the result:

    enter image description here


    And after refactoring the mapping logic into a mapping layer here’s how the corresponding controller action might look like:

    public ActionResult Index()
    {
        // The model could be of any form and come from anywhere but
        // the important thing is that at the end of the day you will have
        // a list of titles here
        DomainModel[] items = ...
    
        // Now let's map this domain model into a view model 
        // that will be adapted to the requirements of our view.
        var viewModel = Mapper.Map<IEnumerable<DomainModel>, IEnumerable<MyViewModel>>(items);
    
        return View(viewModel);
    }
    

    Clean and dry.

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

Sidebar

Related Questions

Say I have a list of items from a database. Next to each item
I have a list of items lets say for example a, b, c. As
I have a ListBox bound to a list of Items (for arguement, lets say
Let's say I have a somewhat large (several millions of items, or so) list
I have a list of say, 20 items. Each of them are boxes in
Let's say I have a class Collection which holds a list of Items. public
Update: Solution below Say you have a list of selected items from Finder. Say
Let's say I have a list of objects: var items = new { new
Lets' say we have list of items, each item has (unknown)number of attributes. Sorting
Lets say you have a list of items: <ul id=ImportantNumbers> <li id=one></li> <li id=two></li>

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.