How to get parent and child table title using nested foreach loop in entity framework

前端 未结 2 448
梦谈多话
梦谈多话 2021-01-26 03:26

I have one Parent table and its child table.I have to display title from parent and child table on page as given below in MVC project:

2条回答
  •  青春惊慌失措
    2021-01-26 04:00

    You can query the children and group them by their parent name. This assumes a ParentClient=>Client relationship.

    var clientGroups = context.Clients.GroupBy(x => x.ParentClient.Name).OrderBy(x=>x.Key).ToList();
    
            foreach (var clientGroup in clientGroups)
            {
                var parentName = clientGroup .Key;
    
                foreach (var child in clientGroup)
                {
                    var title = child.Name;
                }
            }
    

提交回复
热议问题