c#


asp.net mvc c# listing categories recursively


I am trying to fill a list with my categories data.
My categories have cascade format. They are formatted recursively in my database.
Here is my model object.
public class CategoriesDTO
{
public int Id { get; set; }
public int Level { get; set; }
public string Name { get; set; }
public List<CategoriesDTO> Subs { get; set; }
}
So, in my business layer, i am trying to call this method like this:
DAO.Categories(0);
it will start with "Level==0" condition and then it will go on..
but i couldn't manage the Data Access Layer. I tried this:
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
//im stucked
}
return DTO;
}
}
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
item.Subs = ((PrmLevel + 1) <= MaxLevel) ? Categories(PrmLevel + 1) : null;
}
return DTO;
}
}
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
int CountSub = 0;
CountSub = DB.Categories.Where(x => x.Level == item.Id).ToList().Count();
if (CountSub!=0)
{
item.Subs = Categories(item.Id).ToList();
}
}
return DTO;
}
}

Related Links

Delete from one, add to another Listbox and vice versa with the same sorting like before
use unrar.dll in a c# project to retrieve the list of file and relative CRC code
How to declare an array containing generic type?
Manage multiple authentications with ASP.NET Identity
Is there any reason to keep a settings file nested underneath properties?
IIS Web Application can't be accessed remotely
re-number a value by 1 if same value
Fix error with route url in Asp.Net MVC 5
Calling AWS Lambda Fucntion from MVC controller
From C#, how do I call Release on a returned object that implements IStorage?
How to rename folder in network shared folder?
GhostScript has problems to open files with space in their name
Convert some local time to UTC in C#
DirectoryEntry.MoveTo() committing changes without calling CommitChanges()
How to use configuration settings in a Universal Windows App
Displaying an item that meets 2 conditions in a SQL database in a Webform label

Categories

HOME
itunesconnect
netty
primefaces
system-calls
cluster-analysis
intellisense
blob
azure-database-mysql
watson-iot
hiveql
opengl-es
sdk
schema
country-codes
browserstack
virtualhost
subprocess
angularjs-ng-repeat
google-my-business-api
velocity
urllib2
aspectj
osclass
websharper
adroitlogic
android-notifications
github-flavored-markdown
facebook-marketing-api
google-street-view
hl7-fhir
inline-assembly
v4l2
policy
apdu
adobe-captivate
launchd
dynamics-ax-2012-r3
semantic-ui-react
intermec
postgresql-9.6
offset
dropdownbox
ansible-inventory
cowboy
quartz
taiga
api-key
yank
jcs
visio-2010
gige-sdk
xamarin-test-cloud
cc
dmalloc
disassembly
android-tabs
symantec
universe
airwatch
avaudiorecorder
innertext
web-optimization
android-progressbar
nonblocking
php-gd
transport
musicbrainz
photogrammetry
autopep8
esri-arc-engine
method-overloading
qf-test
react-os
drawable
amzi-prolog
visual-studio-setup-proje
banana-pi
pthreads-win32
kuka-krl
mdp
vbaccelerator
mahara
friendly-url
backbone-views
mosix
cron4j
feincms
orchestration
django-1.4
diazo
nserror
jmesa
commonsware
idtabs
parallel-python
azure-appfabric
nyromodal
nscharacterset
activescaffold
silverlight-2.0
dgml
focus-stealing
scala-designer
user-preferences

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App