Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

how to display records from database by creating sublist within a list in asp.net using c#.

for example:
-------------

watches
- wrist watches.
- wall clock watches.


please help me out i am struggling with this code since from long time So please could anyone can help me out.


Thanks & Regards
srikanth.J
Posted

Something like this:

C#
List<watches> wristWatches = (from row in dataTable
                              where row["watchType"] == WatchType.Wrist
                              select row).ToList();
</watches>


Keep in mind this is non-specific code, and you have to add appropriate casting and what-not to make it work in your code.
 
Share this answer
 
v2
Well, let's think of the problem in a different way, shall we?

You're displaying a list of watches with a category. So...group by category, and then sort by whatever is relevant.

I don't have an IDE with me...but something like this should work:

C#
var watches = from w in datacontext.Watches
              group w by w.Category into g
              select new Watch { Category = g.Key, Watches = g };


Now, your watches variable contains x groups of watches (however many categories you have).

You can iterate over this list with a simple foreach as such:

foreach (watchGroup in watches)
{
   // print a header or something
   // Console.WriteLine(watchGroup.Key);
   foreach (watch in watchGroup.Watches)
   {
      // do something here
      // Console.WriteLine(watch.Name);
   }
}


Of course, you'll need to wrap the iteration part above in the appropriate <% %> tag syntax if you're using it on the page. In MVC, you can store the groups into your model on the page or even in the ViewBag.

EDIT: you can get more infos on LINQ here: MSDN[^]

Cheers.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900