Docs Menu
Docs Home
/ /
サポートされているデータ型

辞書 - .NET SDK

バージョン10.2.0の新機能

Realm辞書は、 タイプのキーを持ち、コレクションString を除く任意のRealmタイプの値をサポートする IDIctionary の実装です。辞書を定義するには、 getter 専用の プロパティを使用します。ここでは、IDictionary<string, TValue> TValueはサポートされている型のいずれかです。

オブジェクトの辞書には null オブジェクトを含めることができます。 同様に、プリミティブ型の辞書には null 値を含めることもできます。 辞書で null 値を許可しない場合は、辞書宣言で null 以外の型を使用します(たとえば、IDictionary<string, double> ではなくIDictionary<string, double?> を使用します)。古いスキーマ型の定義(クラスはRealmObject基本クラスから派生している)を使用している場合、または null 可能性が有効になっていない場合は、 stringや null 可能性の高い参照型が含まれている場合は[Required]属性を使用しますbyte[]

Realm では、マップキーに.または$文字を使用できません。 パーセント エンコーディングとデコーディング を使用して、これらの許可されていない文字の 1 つを含むマップキーを保存できます。

重要

同期ではサポートされていない null 値

ローカルのみの Realm は null 可能な(任意)値のコレクションをサポートしていますが、 Syncはサポートしていません。

次のコードは、辞書タイプの例を示しています。

public partial class Inventory : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; }
// The key must be of type string; the value can be
// of any Realm-supported type, including objects
// that inherit from RealmObject or EmbeddedObject
public IDictionary<string, Plant?> Plants { get; }
public IDictionary<string, bool> BooleansDictionary { get; }
// Nullable types are supported in local-only
// Realms, but not with Sync
public IDictionary<string, int?> NullableIntDictionary { get; }
public IDictionary<string, string> RequiredStringsDictionary { get; }
}

次のコードは、string クエリ( RQL )または LINQ を使用して辞書を作成する方法と、辞書に書き込む方法、および辞書から読み取る方法を示しています。

var storeInventory = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory.Plants.Add("Petunia", new Plant());
storeInventory.NullableIntDictionary.Add("random things", 7);
storeInventory.RequiredStringsDictionary.Add("foo", "bar");
var storeInventory2 = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory2.RequiredStringsDictionary.Add("foo", "Bar");
realm.Write(() =>
{
realm.Add(storeInventory);
realm.Add(storeInventory2);
});
// Find all Inventory items that have "Petunia"
// as a key in their Plants dictionary.
var petunias = realm.All<Inventory>()
.Filter("Plants.@keys == 'Petunia'");
// Find all Inventory items that have at least one value in their
// IntDictionary that is larger than 5 using RQL
var matchesMoreThanFive = realm.All<Inventory>()
.Filter("NullableIntDictionary.@values > 5");
// Find all Inventory items where the RequiredStringsDictionary has a key
// "Foo", and the value of that key contains the phrase "bar"
// (case insensitive)
var matches = realm.All<Inventory>()
.Filter("RequiredStringsDictionary['foo'] CONTAINS[c] 'bar'");
// matches.Count() == 2
// Query the Plants dictionary of an Inventory object
// for a specific plant
var myStoreInventory = realm
.All<Inventory>().FirstOrDefault();
var petunia = myStoreInventory.Plants.AsRealmQueryable()
.Where(p => p.Name == "Petunia");

コレクションへの変更を監視するには、辞書の INotifyCollectionchanged.Collectionchangedイベントを使用し、辞書内の特定のプロパティに対する変更を監視するには、 INotifyPropertychanged.Property Changedイベントを使用します。

次のコード例では、 IntDictionaryという名前のIDictionary<string, int>プロパティを持つクラスがあります。 CollectionChangedイベントとPropertyChangedイベントの両方のイベント ハンドラーを設定します。

var dictionary = container.IntDictionary.AsRealmCollection();
dictionary.CollectionChanged += (sender, e) =>
{
Console.WriteLine($"Collection {sender} changed: {e.Action}");
};
dictionary.PropertyChanged += (sender, e) =>
{
Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");
};

戻る

リスト

項目一覧