Lic
Lic

Reputation: 1957

Entity Filter child without include

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!

Upvotes: 0

Views: 241

Answers (2)

Raphael
Raphael

Reputation: 1687

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

Upvotes: 1

SebC
SebC

Reputation: 149

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.

Upvotes: 0

Related Questions