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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:53:18+00:00 2026-06-04T11:53:18+00:00

I have a List of objects with this implementation: class Locations { public string

  • 0

I have a List of objects with this implementation:

 class Locations
    {
        public string Origin { get; set; }
        public string Dest { get; set; }
        public int Total { get; set; }
    }

I need to output a table, in CSV format, that has the Origins as rows, the Destinations as columns, and the Total where the specified Origins and Destinations meet. There could be an arbitrary number of Location objects in my List.

Given the following records in my List:

Origin=A
Dest=B
Total=10

Origin=B
Dest=A
Total=20

My output would look like this (with the ‘-‘ character meaning there’s no Total between identical Origin/Destinations):

Origins/Destinations,A,B
A,-,10
B,20,-

Thus far, I have done the following:

1) Traverse the List to output the Destinations.

2) Traverse the List to output an Origin. For the Origin, traverse the list to find the Total for each Destination related to the Origin.

3) Repeat #2 for the next Origin.

My results aren’t quite working out as the row/column data doesn’t always match up. Is there a simpler solution to 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-06-04T11:53:20+00:00Added an answer on June 4, 2026 at 11:53 am

    Update

    After reading your update it seems that there’s no need to sum as there is only going to be one object with a given Origin and Destination. In that case you can keep the code that collects places, do the nested foreach and simply get the total with

    var item = locations.FirstOrDefault(l => l.Origin == origin && l.Dest == dest);
    var total = item == null ? 0 : item.Total;
    

    Original answer

    You can get a list of all locations with

    var locations = new List<Locations>(); // assume it's populated
    var places = locations.Select(l => l.Origin)
                          .Concat(locations.Select(l => l.Dest))
                          .Distinct()
                          .OrderBy(s => s); // why not sort it as well
    

    At this point you can simply iterate over places for the rows and do another nested iteration for the columns:

    foreach (var origin in places)
    {
        foreach (var dest in places)
        {
            var total = locations.Where(l => l.Origin == origin && l.Dest == dest)
                                 .Sum(l => l.Total);
        }
    }
    

    You can immediately see how you can easily construct a table with this structure. The main disadvantage of this approach is that it’s doing a lot more work than strictly necessary; theoretically we can certainly iterate just once over locations, collecting the information as we go. It’s possible to sum up the Total for each pair of Origin and Dest with

    var totals = locations.GroupBy(l => new { l.Origin, l.Dest })
                 .ToDictionary(g => Tuple.Create(g.Key.Origin, g.Key.Dest),
                               g => g.Sum(r => r.Total));
    

    At this point we can take a page from the first solution:

    foreach (var origin in places)
    {
        foreach (var dest in places)
        {
            var total = totals[Tuple.Create(origin, dest)]; // almost too easy :)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a list of objects. I previously had this
I have the following: class Item { public object ItemId {get;set;} } class StringItem
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a list of objects which I store in the session. This list
I have an array list of objects and I am using this example to
I have a list of System.DirectoryServices.ActiveDirectory.Domain objects gotten via this method. How do I
Background I have a list . This list has many objects. Each object has
I have this situation where I want to display a list of Administration objects
I wrote a linked list implementation for my java class earlier this year. This
I have one class that has a list of objects of Daemon type. class

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.