VB.net Merging Duplicate class items into a list - vb.net

I have a class of variables. To keep it simple I will use car stock as an example. So in my class I have variables for say:-
Car Manufacturer
Car Colour
Car Quantity
and my class looks something like this.. (but potential for hundreds of lines)
BMW, Black, 2
Mercedes, White, 1
Honda, Green, 3
BMW, Red, 1
I need to create a list that merges the manufacturer and the quantity but separates the colours e.g. new list should look something like:-
BMW, 3
Black
Red
Mercedes, 1
White
Honda, 3
Green
Can someone help explain the best way to go about this please?

Assuming that you don't hate one-liners and have some idea of how LINQ works, the following expression will return an array of Tuple(Of String, Integer) where Item1 will have the name of manufacturer and Item2 will have the total number of their cars:
YourCarsList.GroupBy(Function(r) r.Manufacturer).Select(Function(r) New Tuple(Of String, Integer)(r.Key, r.Sum(Function(r2) r2.Quantity))).ToArray()
Note that the output you have shown in the question is console-ish and cannot be returned in that form by a function or expression. If you really want to just print this as text output, you could extend the above expression like this:
Dim Res = YourCarsList.GroupBy(Function(r) r.Manufacturer)
For Each R In Res
output.AppendLine(R.Key, r.Sum(Function(r) r.Quantity))
For Each Clr In R.Select(Function(r) r.Colour).Distinct().OrderBy(Function(r) r)
output.AppendLine(Clr)
Next
Next

Related

SSRS: Summing Lookupset not working

I'm currently working on a report where I'm given 3 different datasets. The report essentially calculates the input, output and losses of a given food production process.
In dataset "Spices", contains the quantity of spices used under a field named "Qty_Spice". In dataset "Meat", contains the quantity of meat used under a field named "Qty_Meat". In dataset "Finished", contains the quantity of finished product used under a field "Qty_Finished".
I'm currently trying to create a table where the amount of input (spice+meat) is compared against output (finished product), such that the table looks like this:
Sum of Inputs (kg) | Finished Product (kg) | Losses (kg)
10 8 2
8 5 3
Total:
18 13 5
What I'm currently doing is using lookupset to get all inputs of both spices and meats (using lookupset instead of lookup because there are many different types of meats and spices used), then using a custom code named "Sumlookup" to sum the quantities lookupset returned.
The problem I'm having is that when I want to get the total sum of all inputs and all finished products (bottom of the table) using "Sumlookup" the table is only returning the first weight it finds. In the example above, it would return, 10, 8 and 2 as inputs, finished products and losses respectively.
Does anyone know how I should approach solving this?
Really appreciate any help
Here is the custom code I used for SumLookUp:
Public Function SumLookup(ByVal items As Object()) As Decimal
Dim suma As Decimal = 0
For Each item As Decimal In items
suma += item
Next
Return suma
End Function

Line wrapping with white space padding using stringtemplate

How can I pad a sentence with white space so that it is printed in a 'block'.
I want to print a receipt. for a given item I want to print the quantity, item name, and price.
12 x Example short name £2.00
1 x This is an example of
a long name £10.00
The 'block' to which I refer to is shown below.
12 x |Example short name | £2.00
1 x |This is an example of |
|a long name | £10.00
Using the example above I can easily handle the formatting of the quantity and the price. Using string formatting. Then for the item name the best method to use, I think, is to split the item name in code, and use the wrap functionality of StringTemplate, but I don't know how to pad the remainder of the line with whitespace
I am using .Net and StringTemplate 4. Here is a simplified version of the template I have. Assuming I have an item with the properties Quantity, ItemName (split into an array of strings), and Price.
<item.Quantity; format="{0,4}"> x <item.ItemName; wrap, separator=" ", anchor><item.Price; format="£{0,8}">
Now at the moment the only way I can think to get it to work is to calculate the white space in code and add it to the item name array.
So is there a neat way to do it in StringTemplate?

write data to oracle database, but first check it in mapping table

Here is what I want to do:
I have 2 tables in oracle database. First one for my data, and second for mapping.
Mapping table has 2 columns and it's like this:
olive - green
marine - green
grass - green
green - green
navy - blue
sky - blue
light blue - blue
etc.
First table (for my data) has more columns, and I should fill every row in that table with various number of colors from mapping table.
and I have txt file with this:
olive,
navy,
green,
#
blue,
sky,
olive,
marine,
#
blue,
light blue,
#
etc.
So, I should basically write that data to my first table in database, but not with those values from txt file, but with adequate value from mapping table.
I hope I explained it well enough. Help pls?

How to compare the value of two/multi column array and add into listbox without dupllication in Visual Basic

Hi I've small project to do and now I'm stuck in middle.
The program is to read the sequential text file and load it into a array/array of structure.
The data information is like this (sample):
ID | Name | Type
1 | Cat | Animal
2 | Dog | Animal
3 | Parrot | Bird
4 | Tuna | Fish
5 | Tiger | Animal
6 | Sparrow | Bird
Sample data in the DATA.txt file looks like this:
1;Cat;Animal
2;Dog;Animal
3;Parrot;Bird
4;Tuna;Fish
5;Sparrow;Bird
6;Tiger;Animal
This is Just as sample data, my original data is more than this.
I've open the DATA.txt file using FileStreamReader:
Dim FileStreamReader As StreamReader = New StreamREader(DATA.txt)
Read all the elements in the list and pass through ReadArrayString.
Split the string using:
DataString.Split(New Char() {";"c})
Pass through
ReadArrayString(0)
ReadArrayString(1)
ReadArrayString(2)
Where index 0 is the ID, index 1 is the name, and index 2 is the Type
Then I load the types in the dropdown combobox menu from the array with out duplication. Like this:
If TypeComboBox.FindString(ReadArrayString(2)) < 0 Then
TypeComboBox.Items.Add(ReadArrayString(2))
End If
Now When you click TypeComboBox it will show drop down menu with following list only.
Animal
Bird
Fish
After this, when Animal type is selected/clicked from combobox dropdown menu then it should only add the Id and Name of Animal type in the ListBox.
Pseudocode may looks like this:
If Animal is selected/clicked from TypeComboBox then
Add Cat into listbox
Add Dog into listbox
Add Tiger into listbox
Else If Bird is selected then
Add Parrot into listbox
Add Sparrow into listbox
Else If Fish is selected then
Add Tuna in to listbox
End If
I've only figure out load items from only one column/array into combobox with out duplication.
But can't figure out to compare the value of one column/array with another column/array and load it into listbox. I don't want to hardcode or write matching value inside the code. What I want is use the Array.
Any help will be great.
Sorry if my explanation is poor. I hope above pseudocode may give you some understanding of my problem.
Unless I am misunderstanding your question, something like this should work, for what you are trying to do.
For Each row as String() In TwoDArray
If row(2) = TypeComboBox.Text Then
listbox.Items.Add(row(1))
End If
Next

Multi-parameter matching + weighted random pick in Redis

Let's say I have a set of objects with properties:
Object Quantity Color Shape Kind
----------------------------------------
APPLE 12 RED ROUND FRUIT
APPLE 3 GREEN ROUND FRUIT
ORANGE 6 ORANGE ROUND FRUIT
CARROT 0 RED CONICAL VEGETABLE
RADISH 24 RED ROUND VEGETABLE
Object and all properties except quantity are represented as strings. Quantity is a number.
I must compose a random list of objects, based on user's query.
Query contains values for all string properties (that is, all properties except quantity).
Value in query may be either exact property value, or a wildcard (meaning "any value would do for this property"), or a negation — "NOT this exact property value".
Query result is an object, picked by weighted random from all object with matching properties. Weight for the random pick is the quantity.
For example:
Query -> Probabilities -> Example
random result
-----------------------------------------------------------------------------
* ROUND FRUIT -> APPLE 12 / APPLE 3 = APPLE 15 -> APPLE
!GREEN ROUND FRUIT -> APPLE 12 / ORANGE 6 -> ORANGE
RED * * -> CARROT 0 / APPLE 12 / RADISH 24
= APPLE 12 / RADISH 24 -> RADISH
RED CONICAL VEGETABLE -> CARROT 0
= (none) -> (none)
For self-education purposes, I would like to build this system using Redis for data storage.
The question is — how to do this elegantly and with least amount of application logic (as opposed to in-Redis operations)? Weights and negation kind of spoil the picture. Otherwise it would be nicely doable with sets.
Any hints are welcome.
Since redis can only query keys and not values, a good option is to store the individual values of each object in seperate redis lists.
For example, when you add the object ...
APPLE 12 RED ROUND FRUIT
you would store it as
hmset obj:1 name apple qty 12 color red shape round kind fruit
and then ...
sadd name:apple obj:1,
sadd color:red obj:1
sadd shape:round obj:1
This way you have a way to interrogate sets directly and be able to pick the object using a random number based on, for example, the total number of items in the set returned.
Hope that helps. If you need more explanation, hit me up.