c# - $select on dynamic property -


i have following code:

public class book {     [key]     public string isbn { get; set; }     public string title { get; set; }     public press press { get; set; }     public idictionary<string, object> properties { get; set; } }  public class bookscontroller : odatacontroller {     private ilist<book> _books = new list<book>     {         new book         {             isbn = "978-0-7356-7942-9",             title = "microsoft azure sql database step step",              properties = new dictionary<string, object>             {                 {"k1","v1"},                 {"k2","v2"}             }         },         new book         {             isbn = "978-0-7356-8383-9",             title = "signalr",             press = new press             {                 name = "microsoft press",                 category = category.book             },             properties = new dictionary<string, object>             {                 {"k1","v1"}             }         }     };      [enablequery]     public iqueryable<book> get()     {         return _books.asqueryable();     } } 

when $select used on dynamic property, result contains lot of empty objects don't contain property. let's in example if query http://localhost:58020/books?$select=k2, getting response as:

{   @odata.context: "http://localhost:58020/$metadata#books(k2)",   value: [    {       k2: "v2"    },    { }   ] } 

if observe contains empty braces book doesn't contain property k2. how rid of behavior?.

thats issue of dictionary. can create new class named parameter , instead of using dictionary<string, object>, use list<parameter> following:

class parameter {     public string key;     public object value; } 

modified code:

public class book {     [key]     public string isbn { get; set; }     public string title { get; set; }     public press press { get; set; }     public list<parameter> properties { get; set; } } public class bookscontroller : odatacontroller {     private ilist<book> _books = new list<book>     {         new book         {             isbn = "978-0-7356-7942-9",             title = "microsoft azure sql database step step",              properties = new list<parameter>             {                 new parameter { key = "k1", value = "v1" },                 new parameter { key = "k2", value = "v2" }             }         },         new book         {             isbn = "978-0-7356-8383-9",             title = "signalr",             press = new press             {                 name = "microsoft press",                 category = category.book             },             properties = new list<parameter>             {                 new parameter { key = "k1", value = "v1" }             }         }     };      [enablequery]     public iqueryable<book> get()     {         return _books.asqueryable();     } } 

Comments