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

Using Linq to check XML files for missing or extra records
read from file 50 chars at a time C# [closed]
How to set dynamic HyperLinks ordered into a table cell
LINQ Select rows with Max of one column and Distinct in another
How to allow Cell edit in DataGrid
Fast Order Statistic Tree in C# or a what DS is more efficient that SortedList providing the same functionality?
Using LINQ, get all the objects from a list with a specific column value? [closed]
How to add items to a DataGridView ComboBoxColumn?
Search a record by Date converting it to UTC, C# SQL Server Entity Framework
Run Azure compute emulator and storage and launch website without visual studio
How to search and replace classes like {classname}Attribute with {classname} classes with Resharper or VS?
Handling CollectionChanged only when the UI modifies the collection
How to set SoapProcessing via code
Get The DataBase Table Name in C#
STA error when opening WPF window from Winform window
Getting System.Runtime.InteropServices.COMException on trying to register word addin

Categories

HOME
excel-formula
postgresql
indexing
botframework
system-calls
gsm
cors
weblogic
dronekit
android-ndk
azure-database-mysql
django-middleware
magento-1.9.2.1
aggregation-framework
jshell
symfony-3.2
revit-api
token
cracking
rename
google-my-business-api
openbsd
watson-dialog
jinja2
lambda-calculus
infopath
multibinding
ofbiz
opengraph
tcpdf
wai-aria
prepros
ecmascript-2017
core-animation
priority-queue
emr
pypy
semantic-ui-react
contextmenustrip
kofax
stencyl
stdin
gem-fury
ssdp
anonymous-function
activeandroid
google-chrome-storage
geminabox
nao-robot
openxava
intermediate-language
openpgp.js
fckeditor
critical-section
huawei
qudpsocket
zsh-completion
virtualenvwrapper
networkstream
xcode7.1
foreground
netlink
vga
angular-i18n
methodology
controlpanel
edgar
persian
consul-template
tooltwist
clearquest
android-networking
page-refresh
system.diagnostics
jeet-grid
service-broker
qxorm
fiware-health
register-allocation
dirname
ccmenuitem
parallel-port
isl
debuggervisualizer
django-tests
opencobol
html5-filesystem
starling-framework
facebook-likebox
playing-cards
wpd
removeall
codeigniter-form-helper
phone-state-listener
linqdatasource
linear-interpolation
flatten
erlide
socketasynceventargs
nsundomanager
youtube.net-api
hwnd
quazip
carbon-emacs
appointment
lts
morfik
opengl-to-opengles
wtsapi32
content-delivery-network
expander
httpbrowsercapabilities

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