0

i'm a C# developer and i have a trouble with Entity Framework 5.

I have mapped my database with Entity using the default code generation strategy. In particolar there are three classes: menus, submenus and submenuitems. The relationships about three classes are: one menu -> to many submenus one submenu -> to many submenuitems.

All classes have a boolean attribute called "Active".

Now, i want to filter all the Menus with the SubMenus active, and the SubMenus with the SubMenuItems active.

To get this i've tried this:

var tmp = _model.Menus.Where(m => m.Active)
                      .Select =>
                      new
                      {
                      Menu = x,
                      SubMenu = x.SubMenus.Where(sb => sb.Active)
                                          .Select(y =>
                                          new
                                          {
                                           SubMenu = y,
                                           SubMenuItem = y.SubMenuItems.Where(sbi => sbi.Active)
                                                                               })
                                          })
                      .Select(x => x.Menu).ToList();

But didn't work.

Someone can help me?

Thank you for your help!

Lic
  • 1,757
  • 3
  • 14
  • 16

2 Answers2

1

Hi have you see this post? Entity Framework: Querying child entities. there are some difference from your code, maybe this helps you.

UPDATE: Projection is when the result of a query is output to a different type than the one queried, it can be to an anonymous type, but could also be to a concrete type. And so using Data transfer object can be usefull to pass data between processes you can read a full explaination here Data Transfer objects

 public class MenuDto
{
    public int MenuId { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public List<MenuDto> SubMenus  { get; set; }
}
_model.Menus.Where(m => m.Active)
            .Select(p => new MenuDto
            {
                MenuId = p.idField,
                Name = p.NameField,
                Url = p.UrlField,
                SubMenus = p.SubMenus.Where(sb => sb.Active)
                    .Select(y => new MenuDto
                    {
                        MenuId = y.idField,
                        Name = y.NameField,
                        Url = y.UrlField,
                        SubMenuItem = y.SubMenuItems.Where(sbi => sbi.Active)
                          .Select(z => new MenuDto
                    {
                        MenuId = z.idField,
                        Name = z.NameField,
                        Url = z.UrlField
                    })
                    })
            }).ToList();

I hope this can solve your problem

Community
  • 1
  • 1
Raphael
  • 1,677
  • 1
  • 15
  • 23
  • Hi! yes i've been see this post. But this istruction: var menus = result.Select(a => a.menus).ToList(); return submenus which are not filtered! – Lic Oct 25 '13 at 09:42
  • It's not just write that line of code. wait I write you the same as the one in the post, I don't know the data returned because I don't have sample data of that kind, but I something like that before – Raphael Oct 25 '13 at 09:50
  • ok, it works. But so i have 3 anonymous types. I would to use menu, submenu, submenuitem types. I have modified your query added the type after new word. But i get an error because i cannot convert the type of submenu (for example) from entitycollection to generic collection. Have to wrap entity object (menu, submenu, submenuitem)? – Lic Oct 25 '13 at 10:20
  • A quick question, does the fields returned from the 3 tables are the same? for example MenuId, MenuName, Url? Maybe you can thinking using Projection, using a data transfer object, it will make your like a little bit easier – Raphael Oct 25 '13 at 10:51
  • Can you explain me the Projection and data trasfer object. The fields doesn't are the same but they can become it. Thanks for all – Lic Oct 25 '13 at 12:00
  • Thanks for your explaination. I think this is the best solution. i will create 3 dto types, one for menu, submenu and submenuitems. Thanks! – Lic Oct 26 '13 at 18:49
0

can you try this:

List<SubMenuItems> tmp =  _model.menus.Where(a => a.active)
    .SelectMany(b => b.SubMenus.Where(a => a.active)).ToList()
    .SelectMany(c => c.SubMenuItems.Where(a => a.active)).ToList();

I hope it's helping.

SebC
  • 139
  • 1
  • 1
  • 8
  • this return submenuitems object and not menu object... i want a list of menu object – Lic Oct 25 '13 at 10:06