You can use LINQ:
using (var context = ww.WebObjs.WebDataContext.Get()) { var menu = context.MenuCaches.Where(x => x.ID == 0).FirstOrDefault(); model = new MenuModel() { ID = menu.ID, URL = menu.Text, ParentID = menu.ParentID, Children = menu.Children.Select(x => new MenuModel { Prop1 = x.Prop1, Prop2 = x.Prop2,
or AutoMapper , which I would highly recommend. Therefore, after determining the appropriate mappings, the action of your controller becomes much more readable:
using (var context = ww.WebObjs.WebDataContext.Get()) { var menu = context.MenuCaches.Where(x => x.ID == 0).FirstOrDefault(); model = Mapper.Map<MenuCache, MenuModel>(menu); }
source share