# Dictionary変数と配列について

**URL:** <https://forum.uipath.com/t/dictionary/768584>\
**Category:** フォーラム\
**Tags:** dictionary, 配列, studio\
**Created:** [September 23, 2024, 12:47am UTC](https://forum.uipath.com/t/dictionary/768584 "2024-09-23T00:47:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![house.of.nf.ssk](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/house.of.nf.ssk/32/297332_2.png) [@house.of.nf.ssk](https://forum.uipath.com/u/house.of.nf.ssk)\
**Post date:** [September 23, 2024, 12:47am UTC](https://forum.uipath.com/t/dictionary/768584/1 "2024-09-23T00:47:32Z")

</div>

◆やりたい事  
データテーブルの各行を配列取得して、Dictionary変数に格納したいです。

◆やりたい事（詳細）  
①テーブルを取得します。test.xlsx(Sheet1)、変数名「Dt」

 ![image](https://global.discourse-cdn.com/uipath/original/4X/7/6/3/763a52f72e9834944ec58366f0609fa78ec2c57d.png)

②データテーブルの各行（繰り返し）アクティビティーで各行ずつ配列に取得  
変数名「Arr」

 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/4/f/c4f69144a2cd4579a30b19a2c4ceb3a4a09a6442.png)

③取得した配列をDictionary変数に格納、変数名「Dic\_項目」

 ![image](https://global.discourse-cdn.com/uipath/original/4X/e/e/c/eec149133fcdcf26e658ec6477b3ba7624ece8c5.png)

（各変数の詳細）

 ![image](https://global.discourse-cdn.com/uipath/original/4X/d/4/2/d42723541e4e715e997f0a8e0103c1fe509398c1.png)  
Arr  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/a/0/5/a05fcc622839a824fa34af781eb48b157f08e6ac.png)  
Dic\_項目  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/9/c/1/9c19a9ebb144f0425922317cf4ca96022f03f871.png)  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/9/a/7/9a72c52b75bbdffe616954da66f4dd53a2300ff1.png)

◆困っていること  
画像でも示している通り、②で配列にテーブルを配列に入れようとするとエラーが出ます。（出力エラー：項目の配列取得: Unable to cast object of type ‘System.Double’ to type ‘System.String’.）  
Doubleがおかしいとのことなのでテーブルを以下のように変更するとうまくいきました。（数字の列を削除しました。）

 ![undefined](https://global.discourse-cdn.com/uipath/original/4X/c/c/2/cc2a4f7391f861a7b00875891b46381a1bd41d30.png)

基本的な事がわかっておらず、情報が不足している点もあるかもしれませんが、原因や対応方法を教えて頂けると助かります。  
最後にXamlファイルを添付しておきます。

[Main.xaml](https://forum.uipath.com/uploads/short-url/nIf3DDMNLXYnn9B7LGN6sJAcZ6O.xaml) (10.3 KB)

---

<div class="post-metadata">

**Author:** ![Yoichi](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/yoichi/32/523754_2.png) [@Yoichi](https://forum.uipath.com/u/Yoichi)\
**Post date:** [September 23, 2024, 2:07am UTC](https://forum.uipath.com/t/dictionary/768584/2 "2024-09-23T02:07:03Z")

</div>

こんにちは

Castの代わりにSelectメソッドで明示的に文字列化するか、object配列で受けるかの何れかになると思います。

前者は

```
Select(Function(o) o.ToString()).ToArray()

```

のようになると思います。

---

<div class="post-metadata">

**Author:** ![house.of.nf.ssk](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/house.of.nf.ssk/32/297332_2.png) [@house.of.nf.ssk](https://forum.uipath.com/u/house.of.nf.ssk)\
**Post date:** [September 23, 2024, 9:59am UTC](https://forum.uipath.com/t/dictionary/768584/3 "2024-09-23T09:59:15Z")

</div>

> [@Yoichi](#):
>
> `Select(Function(o) o.ToString()).ToArray()`

ありがとうございます。  
Arr = CurrentRow.ItemArray().Cast(of String).ToArray()  
ではなく  
Arr = CurrentRow.ItemArray().Select(Function(o) o.ToString()).ToArray()  
にしたところ問題なくテーブルの各行を配列取得でき、Arrに代入できました。

`Cast(of String)` は、各項目が文字列型であることを前提  
それに対して、  
`＝CurrentRow.ItemArray().Select(Function(o) o.ToString()).ToArray()` は、  
`ToString()` メソッドがすべてのオブジェクトに対して効いて、各項目を文字列に変換するため成功したと理解しました。データ型に関係なくすべての項目が文字列として扱われる上記の書き方を今後は使用しようと思います。大変助かりました。ありがとうございました。

---

<div class="post-metadata">

**Author:** ![gorby](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/gorby/32/543563_2.png) [@gorby](https://forum.uipath.com/u/gorby)\
**Post date:** [September 23, 2024, 11:25am UTC](https://forum.uipath.com/t/dictionary/768584/4 "2024-09-23T11:25:32Z")

</div>

> [@house.of.nf.ssk](#):
>
> Dictionary変数に格納したいです。

すみませんが、Dictionary変数に格納された後はどのような処理をされるのでしょうか。ご参考までに教えてください。

---

<div class="post-metadata">

**Author:** ![house.of.nf.ssk](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/house.of.nf.ssk/32/297332_2.png) [@house.of.nf.ssk](https://forum.uipath.com/u/house.of.nf.ssk)\
**Post date:** [September 23, 2024, 12:28pm UTC](https://forum.uipath.com/t/dictionary/768584/5 "2024-09-23T12:28:08Z")

</div>

この課題の最終は単純にメッセージボックスに「Dic\_項目(“ID0005”)(2)」などとして完了します。

 ![The image depicts a UiPath workflow for iterating through each row of a data table, extracting and converting the row data to a string array, storing it in a dictionary with the row's ID as the key, and displaying a message box containing the value of a specific dictionary element. (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/a/6/5/a656bceeb5099bbbc3a99d80807070bc6a71c4cd.png)

ただもっと背景（本来の課題）を言えば下記の画像のようなことをしたかったんです。  
（商品名１，２があるが、隣のNoが最大のものをテーブルに残し重複行を削除）

 ![The image shows two tables, one with detailed entries for products and their numbers, and another summarizing the greatest number for each product, with a blue arrow pointing from the detailed to the summary table. (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/c/7/0/c70755974617a48a4ca815b0a67692c262da17cb.png)

そこでは組み合わせで考えるべきだからDictionary変数の知識が必要かと思い勉強している途中で躓きました。

但しこの課題は、下図のように重複なしのテーブルを作ってフィルタリングと並べ替え、２行目以降は削除の方法で解決できるかもしれず、そちらはそちらで模索中です。（まだ纏まっていませんが、、、）

 ![The image shows a snippet of code written in Japanese, which is creating a data table "UniqueValue" to store unique product names from the data table "dt". (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/9/8/7/987e5b42d0a4b544cb86c98b953422de42fa3361.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [September 26, 2024, 12:28pm UTC](https://forum.uipath.com/t/dictionary/768584/6 "2024-09-26T12:28:21Z")

</div>

Closed on September 26, 2024, 12:28pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
