Author Topic: A bit lost... List Items & Tuples  (Read 2168 times)

0 Members and 1 Guest are viewing this topic.

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
A bit lost... List Items & Tuples
« on: February 16, 2019, 11:44:12 AM »
Hey everyone... Forgive me if this ends up getting more complicated than it should. I haven't had a real need for anything like this before and a bit lost. I am trying to figure out how I would go about doing a few things. So...

Code: [Select]
Dim _List As New List(Of Tuple(Of Double, String, String, Double, Integer))

For Each _ObjID In _Objects
                Dim _CurrentObject As Object= _Trans.GetObject(_ObjID, OpenMode.ForRead)

                _ObjProperties = New Tuple(Of Double, String, String, Double, Integer)(_CurrentObject.Prop1, _CurrentObject.Prop2, _CurrentObject.Prop3, _CurrentObject.Prop4, _CurrentObject.Prop5)

                _List.Add(_ObjProperties)
Next

Simple, no issues... For example, if I returned each item in _List, I would end up with something like this:

Quote
(1.00, "A", "B", 100.00, 1)
(2.00, "A", "B", 100.00, 1)
(1.00, "A", "B", 100.00, 1)
(9.00, "C", "A", 100.00, 1)
(1.00, "C", "A", 100.00, 1)
(2.00, "A", "B", 100.00, 1)
(1.00, "A", "B", 100.00, 1)
(9.00, "C", "A", 100.00, 1)

What I need to do now though is the part I am lost with... I want to take my _List and combine items that match certain criteria...

For example, I want to combine all items that have the same values for _CurrentObject.Prop1, _CurrentObject.Prop2, _CurrentObject.Prop3, but I want to do something like add/subtract the values for _CurrentObject.Prop4, _CurrentObject.Prop5... Not sure if that sounds right, but... This would be what the List looks like in the end...

Quote
(1.00, "A", "B", 300.00, 3)
(2.00, "A", "B", 200.00, 2)
(9.00, "C", "A", 200.00, 2)
(1.00, "C", "A", 100.00, 1)

Does this make sense? If so, anyone have any example of how I could go about something like this? I am thinking that a For Each and If statement combo might be the right route, but I am drawing a complete blank right now...
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: A bit lost... List Items & Tuples
« Reply #1 on: February 16, 2019, 12:30:17 PM »
Hi,

Linq is your friend.

You can use the 'query expression' syntax
Code - C#: [Select]
  1. IEnumerable<Tuple<double, string, string, double, int>> query =
  2.     from t in _List
  3.     group t by t.Item1 + t.Item2 + t.Item3
  4.     into g
  5.     select new Tuple<double, string, string, double, int>(g.First().Item1, g.First().Item2, g.First().Item3, g.Sum(t => t.Item4), g.Count())

Or (my preference) the 'method based' syntax with lambda expressions
Code - C#: [Select]
  1. var query = _List
  2.     .GroupBy(t => t.Item1 + t.Item2 + t.Item3)
  3.     .Select(g => new Tuple<double, string, string, double, int>(g.First().Item1, g.First().Item2, g.First().Item3, g.Sum(t => t.Item4), g.Count()));

IMHO, instead of a 5 items tuple, you should use a litlle class or structure to store the data, this should make your code clearer.
Speaking English as a French Frog

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
Re: A bit lost... List Items & Tuples
« Reply #2 on: February 16, 2019, 03:19:44 PM »
gile, You rock! This worked out perfectly. I tried both methods you mentioned, and I think I can see why you like the method based syntax. At first I wasn't even thinking I would have a need for a separate class, but once I started tying the pieces together, it did just as you suggested and is making my day much easier. Thank you!
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017