Search for existing suggestions

Richer collection support, including ordered collections and dictionaries of value types

It would be very nice to be able to support properties which are collections of value types. Just to have an array of doubles I shouldn't need to map each value to a class. NHibernate has this feature. E.g.
class Benchmark
{
public long Id {get; set;}
public string Name {get; set;}
public IDictionary<DateTime, double> Returns {get; set;}
public int[] Vector {get; set;} // preserve the order too
}

65 votes
Sign in
(thinking…)
Password icon
Signed in as (Sign out)

We’ll send you updates on this idea

Val shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

6 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • CBke commented  ·   ·  Flag as inappropriate

    implementing IDictionary even prevents a simple 1-many (List) from being mapped

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    namespace dict.Models
    {
    public class DictItem
    {
    [Key]
    public int Key { get; set; }
    public string Value { get; set; }
    }
    public class DictList
    {
    public int DictListId { get; set; }
    public List<DictItem> MyFakeDict { get; set; }
    }
    public class Dict : IDictionary<int, string>
    {
    public int DictId { get; set; }
    public List<DictItem> MyDict { get; set; }

    public void Add(int key, string value)
    {
    if (!ContainsKey(key))
    MyDict.Add(new DictItem { Key = key, Value = value });
    else
    throw new System.ArgumentException("An item with the same key has already been added.");
    }

    public bool ContainsKey(int key)
    {
    foreach (int Key in Keys)
    if (Key == key)
    return true;

    return false;
    }

    public ICollection<int> Keys
    {
    get
    {
    List<int> languagelist = new List<int>();
    foreach (DictItem item in MyDict)
    languagelist.Add(item.Key);

    return languagelist;
    }
    }

    public bool Remove(int key)
    {
    foreach (DictItem item in MyDict)
    {
    if (item.Key == key)
    return MyDict.Remove(item);
    }
    return false;
    }

    public bool TryGetValue(int key, out string value)
    {
    foreach (DictItem item in MyDict)
    {
    if (item.Key == key)
    {
    value = item.Value;
    return true;
    }
    }
    value = default(string);
    return false;
    }

    public ICollection<string> Values
    {
    get
    {
    ICollection<string> itemlist = new List<string>();
    foreach (DictItem item in MyDict)
    itemlist.Add(item.Value);

    return itemlist;
    }
    }

    public string this[int key]
    {
    get
    {
    foreach (DictItem item in MyDict)
    {
    if (item.Key == key)
    return item.Value;
    }
    return default(string);
    }
    set
    {
    if (!ContainsKey(key))
    Add(key, value);
    }
    }

    public void Add(KeyValuePair<int, string> item)
    {
    Add(item.Key, item.Value);
    }

    public void Clear()
    {
    MyDict.Clear();
    }

    public bool Contains(KeyValuePair<int, string> item)
    {
    string founditem;
    if (TryGetValue(item.Key, out founditem))
    return founditem.Equals(item.Value);

    return false;
    }

    public void CopyTo(KeyValuePair<int, string>[] array, int arrayIndex)
    {
    if (array == null)
    throw new System.ArgumentNullException("array is null.");
    if (arrayIndex < 0)
    throw new System.ArgumentOutOfRangeException("index is less than zero.");
    if (array.Length - arrayIndex < this.Count)
    throw new System.ArgumentException("The number of elements in the source System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection is greater than the available space from index to the end of the destination array.");
    for (int i = 0; i < MyDict.Count; i++)
    {
    array[i + arrayIndex] = new KeyValuePair<int, string>(MyDict[i].Key, MyDict[i].Value);
    }
    }

    public int Count
    {
    get { return MyDict.Count; }
    }

    public bool IsReadOnly
    {
    get { return false; }
    }

    public bool Remove(KeyValuePair<int, string> item)
    {
    return Remove(item.Key);
    }

    public IEnumerator<KeyValuePair<int, string>> GetEnumerator()
    {
    throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
    throw new NotImplementedException();
    }
    }
    public class MyClass
    {
    public int MyClassId { get; set; }
    public DictList MyDictList { get; set; }
    public Dict MyDict { get; set; }
    }
    }

  • Lenne commented  ·   ·  Flag as inappropriate

    Something like

    modelBuilder.Entity<Element>().HasMany(e => e.Children).OrderedBy(e => e.Position).WithOptional(e => e.Parent);

    would be nice!

  • Ashish commented  ·   ·  Flag as inappropriate

    I think you should have this feature, i am surprised people havent asked for it, its very useful in lookup tables scenarios, we have about 20 lookup tables, and without this our object graphs are exploding

  • Ciel commented  ·   ·  Flag as inappropriate

    Please, please implement this feature. I want this almost as much as base class support.

Feedback and Knowledge Base