c# - Entity Framework: Selecting distinct objects -


i using entity framework data access , manipulation. have following class:

public class productcategory {     [key]     [databasegenerated(databasegeneratedoption.identity)]     public int id { get; set; }      [required]     [minlength(5)]     [maxlength(45)]     public string name { get; set; }      [column("parentid", typename = "int")]     public int? productcategoryid { get; set; }     public virtual productcategory parent { get; set; }     public icollection<productcategory> subcategories { get; set; } } 

now querying using linq list of categories, including related child categories (using parentid column) this:

public async task<iqueryable<productcategory>> getall() {     return await task.run(() => {         return _db.productcategories.asqueryable();     }); } 

when run code, following result:

[   {     "id": 1,     "name": "computers & software",     "parent": null,     "subcategories": [       {         "id": 2,         "name": "tablets & e-readers",         "subcategories": [           {             "id": 8,             "name": "apple ipad",             "subcategories": []           },           {             "id": 9,             "name": "samsung",             "subcategories": []           },           {             "id": 10,             "name": "other makes",             "subcategories": []           }         ]       }     ]   },   {     "id": 2,     "name": "tablets & e-readers",     "parent": {       "id": 1,       "name": "computers & software",       "parent": null     },     "subcategories": [...]   } ] 

now, thing second object name "tablets & e-readers" exists in array of "subcategories" within first object of array. not see second object (distinct), cannot working. know how tackle issue?

i think there wrong data. subcategory (tablets & e-readers) should not have null parent have in data now. subcategory mean there parent. if want parent category, can try this:

_db.productcategories.where(cat=>cat.parent == null).asqueryable(); 

may want. it'will not select subcategories in main list.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -