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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:05:50+00:00 2026-06-12T22:05:50+00:00

I am having trouble with grouping a list, and then building a model that

  • 0

I am having trouble with grouping a list, and then building a model that represents that list and displaying the results in a table within a view. For example:

List of items ordered

  1. Date
  2. CustomerId
  3. Location
  4. Item
  5. Price
  6. Quantity

How would I correctly model and display that list in a table if I wanted to group the list by location? And what if I wanted to group the list by two properties, for example, Location and CustomerId?

Here is my model:

public class ViewInvoice
{
public string ClientLocation { get; set; }
public List<DetailsGroup> Details { get; set; }

public class DetailsGroup
{
    public List<string> Product { get; set; }
    public List<string> ProductSize { get; set; }
    public List<string> PackageType { get; set; }
    public List<DateTime> OrderDate { get; set; }
    public List<DateTime> DeliveryDate { get; set; }
    public List<int> OrderNumber { get; set; }
    public List<decimal> Price { get; set; }
    public List<int> ItemQuantity { get; set; }
}
}   

I am trying to display this model in a table within my razor view. Here is that code:

@using MyModel.MyTools.Orders.SumOrder
@model SumOrder

@{
ViewBag.Title = "View Invoice";
}

<h2>View Invoice</h2>

<table>
    @foreach(var prod in Model.OCI)
    {
        <tr>
            <td>
                @prod.ClientLocation
            </td>
        </tr>
         foreach (var orderItem in prod.Details)
        {
            <tr>
                <td>
                    @orderItem.Product
                </td>
                <td>
                    @orderItem.ItemQuantity
                </td>
            </tr>
        }
    }
</table>

The first row in the table displays correctly, which is the name of a city, but in the next row I get this:

System.Collections.Generic.List1[System.String] System.Collections.Generic.List1[System.Int32]

Can someone explain to me why I can not get the list returned in a readable format, and how to correct this problem?

Here is the code I used to group the list for the ViewInvoice model:

public SumOrder(List<orders_Cart> order)
    {
        // create list of order cart item
        List<OrderCartItems> cartItems = new List<OrderCartItems>();

        // convert orders to ocm
        foreach(var item in order)
        {
            var newCartItem = new OrderCartItems();
            try
            {
                newCartItem.Product = db.product_Product.FirstOrDefault(p =>
                    p.Id == item.ProductId).ProductDescription ?? "none";
            }
            catch (Exception)
            {

                newCartItem.Product = "none";
            }
            try
            {
                newCartItem.ClientForProduct = MyTool.OrdersFindClientLocation(
                    (int) item.ClientForOrdersId);
            }
            catch (Exception)
            {

                newCartItem.ClientForProduct = new object[3];
            }
            try
            {
                newCartItem.ProductSize = db.products_Size.FirstOrDefault(p => p.Id ==
                    item.ProductSizeId).ProductSizeCode ?? "none";
            }
            catch (Exception)
            {

                newCartItem.ProductSize = "none";
            }
            try
            {
                newCartItem.PackageType = db.packaging_PackageType.FirstOrDefault(p =>
                    p.Id == item.PackageTypeId).PackageTypeCode ?? "none";
            }
            catch (Exception)
            {

                newCartItem.PackageType = "none";
            }
            newCartItem.OrderDate = (DateTime) item.OrderDate;
            newCartItem.DeliveryDate = (DateTime) item.DeliveryDate;
            newCartItem.OrderNumber = (int) item.OrderNumber;
            newCartItem.Price = (decimal) item.Price;
            newCartItem.ClientLocation = MyTool.OrdersFindClientLocation(
                (int) item.ClientForOrdersId, null);
            newCartItem.ItemQuantity = (int) item.Quantity;
            cartItems.Add(newCartItem);
        }

        // group the cartItems according to location
        List<ViewInvoice> ordersGrouped = cartItems.GroupBy(c => new 
            {c.ClientLocation})
            .OrderBy(c => c.Key.ClientLocation).Select(s =>
                new ViewInvoice()
                    {
                        ClientLocation = s.Key.ClientLocation,
                        Details = new List<ViewInvoice.DetailsGroup>()
                            {
                                new ViewInvoice.DetailsGroup()
                                    {
                                        Product = s.Select(p => p.Product).ToList(),
                                        ItemQuantity = s.Select(p => p.ItemQuantity).ToList(),
                                        DeliveryDate = s.Select(p => p.DeliveryDate).ToList(),
                                        OrderDate = s.Select(p => p.OrderDate).ToList(),
                                        OrderNumber = s.Select(p => p.OrderNumber).ToList(),
                                        PackageType = s.Select(p => p.PackageType).ToList(),
                                        Price = s.Select(p => p.Price).ToList(),
                                        ProductSize = s.Select(p => p.ProductSize).ToList()
                                    }

                            }

                    }).ToList();

        // set the OCI property
        OCI = ordersGrouped;
    };
  • 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-12T22:05:52+00:00Added an answer on June 12, 2026 at 10:05 pm

    Ok, I finally solved my problem. I initially over-thought the problem. I simplified my model, and added some simple logic to my view.

    Here is the updated Model:

    public class ViewInvoice
    {
        public string ClientLocation { get; set; }
        public List<string> Product { get; set; }
        public List<string> ProductSize { get; set; }
        public List<string> PackageType { get; set; }
        public List<DateTime> OrderDate { get; set; }
        public List<DateTime> DeliveryDate { get; set; }
        public List<int> OrderNumber { get; set; }
        public List<decimal> Price { get; set; }
        public List<int> ItemQuantity { get; set; }
    }
    

    the updated code used to group the list for the Model:

    // group the cartItems according to location
            List<ViewInvoice> ordersGrouped = cartItems.GroupBy(c => new 
                {c.ClientLocation})
                .OrderBy(c => c.Key.ClientLocation).Select(s =>
                    new ViewInvoice()
                        {
                            ClientLocation = s.Key.ClientLocation,
                            Product = s.Select(p => p.Product).ToList(),
                            ItemQuantity = s.Select(p => p.ItemQuantity).ToList(),
                            DeliveryDate = s.Select(p => p.DeliveryDate).ToList(),
                            OrderDate = s.Select(p => p.OrderDate).ToList(),
                            OrderNumber = s.Select(p => p.OrderNumber).ToList(),
                            PackageType = s.Select(p => p.PackageType).ToList(),
                            Price = s.Select(p => p.Price).ToList(),
                            ProductSize = s.Select(p => p.ProductSize).ToList()
    
                        }).ToList();
    

    and the updated view:

    @using MyModel.MyTools.Orders.SumOrder
    @model SumOrder
    
    @{
        ViewBag.Title = "View Invoice";
    }
    
    <h2>View Invoice</h2>
    @{
        int i = 0;
    }
    <table>
        @foreach(var mod in Model.OCI)
        {
            var modCount = @mod.Product.Count();
            <tr>
                <th>@mod.ClientLocation</th>
            </tr>
            <tr>
                <th>Product</th>
                <th>Price</th>
            </tr>
            foreach (var items in mod.Product)
            {
                <tr>
                    <td>
                        @mod.Product.ElementAtOrDefault(i)
                    </td>
                    <td>
                        @mod.Price.ElementAtOrDefault(i)
                    </td>
                </tr>
                i++;
            }
    
        }
    </table>    
    

    This solution clearly allows me to iterate through the model reproducing any required rows or cells along the way. Played Russian roulette for two days over this problem. Hope this saves some others some time.

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

Sidebar

Related Questions

I'm having trouble grouping a set of nodes. I've found an article that does
Having trouble grouping active forum sum results by wildcards. Ultimately, I want all the
Having trouble with each function... Will try to explain by example... In my code,
Having trouble understanding how to filter an images table by tag information in a
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having trouble using foreach to output the contents of a mysql table. The table
Having trouble trying to turn an XDocument into an list of objects, in particular
Having trouble looping through multiple rows in a SQL table and getting the info
LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm
I'm experimenting with Linq and am having trouble figuring out grouping. I've gone through

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.