Condividi tramite


bag_pack()

Crea un oggetto contenitore di proprietà dinamico da un elenco di chiavi e valori.

Alias deprecati: pack(), pack_dictionary()

Sintassi

bag_pack(key1 value1,, key2 value2, ,... )

Altre informazioni sulle convenzioni di sintassi.

Parametri

Nome Digita Obbligatorio Descrizione
key string ✔️ Nome della chiave.
value qualsiasi tipo di dati scalare ✔️ Valore della chiave.

Nota

Le stringhe chiave e valore sono un elenco alternato la lunghezza totale dell'elenco deve essere pari.

Valori restituiti

Restituisce un dynamic oggetto contenitore delle proprietà dagli input di chiave e valore elencati.

Esempi

Esempio 1

Nell'esempio seguente viene creato e restituito un contenitore di proprietà da un elenco alternato di chiavi e valori.

print bag_pack("Level", "Information", "ProcessID", 1234, "Data", bag_pack("url", "www.bing.com"))

Risultati

print_0
{"Level":"Information","ProcessID":1234,"Data":{"url":"www.bing.com"}}

Esempio 2

L'esempio seguente crea un contenitore delle proprietà ed estrae il valore dal contenitore delle proprietà usando l'operatore '.'.

datatable (
    Source: int,
    Destination: int,
    Message: string
) [
    1234, 100, "AA", 
    4567, 200, "BB",
    1212, 300, "CC" 
]
| extend MyBag=bag_pack("Dest", Destination, "Mesg", Message)
| project-away Source, Destination, Message
| extend MyBag_Dest=MyBag.Dest, MyBag_Mesg=MyBag.Mesg

Risultati

MyBag MyBag_Dest MyBag_Mesg
{"Dest":100,"Mesg":"AA"} 100 AA
{"Dest":200,"Mesg":"BB"} 200 BB
{"Dest":300,"Mesg":"CC"} 300 CC

Esempio 3

Nell'esempio seguente vengono utilizzate due tabelle, SmsMessages e MmsMessages e vengono restituite le colonne comuni e un contenitore di proprietà dalle altre colonne. Le tabelle vengono create ad hoc come parte della query.

SmsMessages

SourceNumber TargetNumber CharsCount
555-555-1234 555-555-1212 46
555-555-1234 555-555-1213 50
555-555-1212 555-555-1234 32

MmsMessages

SourceNumber TargetNumber AttachmentSize AttachmentType AttachmentName
555-555-1212 555-555-1213 200 JPEG Pic1
555-555-1234 555-555-1212 250 JPEG Pic2
555-555-1234 555-555-1213 300 png Pic3
let SmsMessages = datatable (
    SourceNumber: string,
    TargetNumber: string,
    CharsCount: string
) [
    "555-555-1234", "555-555-1212", "46", 
    "555-555-1234", "555-555-1213", "50",
    "555-555-1212", "555-555-1234", "32" 
];
let MmsMessages = datatable (
    SourceNumber: string,
    TargetNumber: string,
    AttachmentSize: string,
    AttachmentType: string,
    AttachmentName: string
) [
    "555-555-1212", "555-555-1213", "200", "jpeg", "Pic1",
    "555-555-1234", "555-555-1212", "250", "jpeg", "Pic2",
    "555-555-1234", "555-555-1213", "300", "png", "Pic3"
];
SmsMessages 
| join kind=inner MmsMessages on SourceNumber
| extend Packed=bag_pack("CharsCount", CharsCount, "AttachmentSize", AttachmentSize, "AttachmentType", AttachmentType, "AttachmentName", AttachmentName) 
| where SourceNumber == "555-555-1234"
| project SourceNumber, TargetNumber, Packed

Risultati

SourceNumber TargetNumber Gremito
555-555-1234 555-555-1213 {"CharsCount":"50","AttachmentSize":"250","AttachmentType":"jpeg","AttachmentName":"Pic2"}
555-555-1234 555-555-1212 {"CharsCount":"46","AttachmentSize":"250","AttachmentType":"jpeg","AttachmentName":"Pic2"}
555-555-1234 555-555-1213 {"CharsCount":"50","AttachmentSize":"300","AttachmentType":"png","AttachmentName":"Pic3"}
555-555-1234 555-555-1212 {"CharsCount":"46","AttachmentSize":"300","AttachmentType":"png","AttachmentName":"Pic3"}