Cartesian join of two arrays - vb.net-2010

I am trying to use a for next loop to iterate through the arrays and combine its strings
I can only get the first array. Don't know how to code to combine with the second one and create the third array using VB.NET. could you help please?
ex:
arrLetters() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I"}
arrNumbers() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
resulting array(81) = {A1, A2 ...A9, B1, B2...B9, ...I9}

In C# it would be:
from letter in arrLetters
from number in arrNumbers
select letter + number
In VB, with the result going in to an array variable:
Dim array = (From letter In arrLetters
From number In arrNumbers
Select letter + number).ToArray()

Use below logic (In C#)
var arrLetters= new string[] {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
var arrNumbers = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var array = arrLetters.Zip(arrNumbers, (letter, word) => letter + word);
Hope this helps :)

Related

Filtering out objects from multiple arrays in a JSONB column

I have a JSON structure with two arrays saved in a JSONB column. A bit simplified it looks like this
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
Schema:
CREATE TABLE campaign
(
id TEXT PRIMARY KEY,
data JSONB
);
Since each row (data column) can be fairly large, I'm trying to filter out matching item objects and group objects from the items and groups arrays.
My current query is this
SELECT * FROM campaign
WHERE
(data -> 'items' #> '[{"productId": "123"}]') OR
(data -> 'groups' #> '[{"groupId": "B"}]')
which returns rows containing either the matching group or the matching item. However, depending on the row, the data column can be a fairly large JSON object (there may be hundreds of objects in items and tens in groups and I've omitted several keys/properties for brevity in this example) which is affecting query performance (I've added GIN indexes on the items and groups arrays, so missing indices is not why it's slow).
How can I filter out the items and groups arrays to only contain matching elements?
Given this matching row
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
I'd like the result to be something like this (the matching item/group could be in different columns from the rest of the data column - doesn't have to be returned in a single JSON object with two arrays like this, but I would prefer it if doesn't affect performance or lead to a really hairy query):
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
}
],
"groups": [
{
"groupId": "B"
"discount": "20",
"discountId": "3"
}
]
}
What I've managed to do so far is unwrap and match an object in the items array using this query, which removes the 'items' array from the data column and filters out the matching item object to a separate column, but I'm struggling to join this with matches in the groups array.
SELECT data - 'items', o.obj
FROM campaign c
CROSS JOIN LATERAL jsonb_array_elements(c.data #> '{items}') o(obj)
WHERE o.obj ->> 'productId' = '124'
How can I filter both arrays in one query?
Bonus question: For the groups array I also want to return the object with the lowest discount value if possible. Or else the result would need to be an array of matching group objects instead of a single matching group.
Related questions: How to filter jsonb array elements and How to join jsonb array elements in Postgres?
If your postgres version is 12 or more, you can use the jsonpath language and functions. The query below returns the expected result with the subset of items and groups which match the given criteria. Then you can adapt this query within a sql function so that the search criteria is an input parameter.
SELECT jsonb_set(jsonb_set( data
, '{items}'
, jsonb_path_query_array(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")'))
, '{groups}'
, jsonb_path_query_array(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")'))
FROM (SELECT
'{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}' :: jsonb) AS d(data)
WHERE jsonb_path_exists(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")')
AND jsonb_path_exists(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")')

Appending a new array to a JSON object with SQL Server

I would like to append an array to a JSON object:
JSON object:
{ "value1": "test" }
Array to be appended:
{ "array2": ["1", "2", "3"] }
Expected result:
{ "value1": "test", "array2": ["1", "2", "3"] }
My attempts with JSON_MODIFY failed:
Attempt #1:
SELECT
JSON_MODIFY('{ "value1": "test" }',
'append $.array2',
JSON_QUERY('[ "1", "2", "3" ]'))
-- { "value1": "test", "array2": [["1", "2", "3"]] }
-- An array within an array is appended
Attempt #2:
SELECT
JSON_MODIFY('{ "value1": "test" }',
'append $',
JSON_QUERY('{"array2": [ "1", "2", "3" ]}'))
-- { "value1": "test" }
-- Result doesn't contain the array at all
You are trying to set a property, not appending items to array so remove the append keyword:
SELECT JSON_MODIFY(
'{"value1": "test"}',
'$.array2',
JSON_QUERY('["1", "2", "3"]')
)
-- {"value1": "test","array2":["1", "2", "3"]}

Kotlin - How do I sum specific element in list?

val total = mutableListOf("NO", "NO", "1", "2", "3", "4")
I want to sum which only can cast to int
In this case "1", "2", "3", "4"
Is there any quick way in Kotlin?
you can just use sumBy like this:
total.sumBy { it.toIntOrNull() ?: 0 }

how to convert the query result data to json format in kusto query language

I'm working on a logic app which will create a bug I have to display description in the work item like below:
{
"Description":
{
"Title":"",
"Validation step":"",
"OperationActivityId:":"",
"Environment detailsLink":"",
"Please click here":"",
"ErrorMessage":""
}
}
I'm Querying all above mentioned fields as result of query from Kusto(KQL) and getting all the required fields but I don't know how to convert it to make it Json.
Can anyone help me on this..?
Thanks in advance.
Here you go:
PackedRecordlet MyTable = datatable(Category:string, Value:long) [
"A", 1,
"A", 3,
"A", 5,
"B", 1,
"B", 2
];
MyTable
| extend PackedRecord = pack_all()
| summarize Result = make_list(PackedRecord)
This will output everything in a nice json:
[
{
"Category": "A",
"Value": 1
},
{
"Category": "A",
"Value": 3
},
{
"Category": "A",
"Value": 5
},
{
"Category": "B",
"Value": 1
},
{
"Category": "B",
"Value": 2
}
]
Explanation
pack_all() puts all the data from the columns of a record into a JSON
summarize make_list(...) takes all these JSONs, and creates a list (array) from them

How to iterate and render my fetch result in react native JSX

{
"status": true,
"live_score_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"live_score_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"results_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"results_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"fixture_point_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12"
}
],
"fixture_point_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24"
}
]
}
This is the fetch result.I want to render first a heading like domestic Tournaments.Then i want to loop for al tornaments in domestic.Then all matches in each tournament.How i can do this?Anyone to help.thanks in advance :)
can you please help me to figure out how to iterate all
First of all you should decide on how the data should be displayed on the screen. Should they be clickable or not, scrolled or not, what part of screen they should take etc. And depending on the visual design of your future application you can make a choice what exact react-native visual component will represent the data on the screen by the best way.
For example, if it is applicable by visual design of your application you can take a look at the react-native ListView or ScrollView components to render endless list of identical complex data.
If iterating through that entire JSON object is your intent, then an easy approach is a for...in loop.
For example:
var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
function walk(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var val = obj[key];
console.log(val);
walk(val);
}
}
}
walk(obj);
Taken from: iterating through json object javascript
Object.keys(data).map((key) => { ... })
This will use the object properties (live_score_domestic, live_score_international, etc) to iterate through the top most data. Then you can use data[key] to get throught its contents.