user9582479
user9582479

Reputation: 83

Cannot initialize type with a collection initializer because it does not implement 'System.Collections.IEnumerable'

I have 3 entities for main menus. and sub menus. I created menu view model.

My Entities:

MainMenu.cs:

public UstMenuler()
{
    AltMenuler = new HashSet<AltMenuler>();
}

public int ID { get; set; }
public string Name { get; set; }
public bool AktifMi { get; set; }
public int YetkiID { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public virtual Yetkilendirme Yetkilendirme { get; set; }
public virtual ICollection<AltMenuler> AltMenuler { get; set; }

SubMenu.cs:

public partial class AltMenuler
{      
    public AltMenuler()
    {
        UstMenuler = new HashSet<UstMenuler>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }
    public int? YetkiID { get; set; }
    public string Name { get; set; }
    public string ActionName { get; set; }
    public string ControllerName { get; set; }
    public virtual Yetkilendirme Yetkilendirme { get; set; }
}     

MyViewModel:

public class MenuViewModel
{
    public UstMenuler UstMenuler { get; set; }
    public AltMenuler AltMenuler { get; set; }
}

My Controller:

dbContext = new MarkettingDbContext();

int sessionYetkiID = (int)Session["yetkiID"];
MenuViewModel menuler = new MenuViewModel();

var query = (from menu in dbContext.UstMenuler
             where (menu.YetkiID == sessionYetkiID)
             select new MenuViewModel
             {
                 menuler.UstMenuler.ID = menu.ID,
                 menuler.UstMenuler.Name = menu.Name,
                 menuler.UstMenuler.YetkiID = menu.YetkiID,
                 menuler.UstMenuler.ControllerName = menu.ControllerName,
                 menuler.UstMenuler.ActionName = menu.ActionName
             }).ToList();

I am getting error when I execute my query. For example my sessionID is 1, I want to take my main and submenus.

Upvotes: 2

Views: 9723

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500675

The problem is your object initializer:

select new MenuViewModel
{
    menuler.UstMenuler.ID = menu.ID,
    menuler.UstMenuler.Name = menu.Name,
    menuler.UstMenuler.YetkiID = menu.YetkiID,
    menuler.UstMenuler.ControllerName = menu.ControllerName,
    menuler.UstMenuler.ActionName = menu.ActionName
}

Firstly you're using menuler in the object initializer, and I'm not sure why. Next, you're trying to assign to subproperties, which even if it did compile, would result in a NullReferenceException. Collectively, that's basically not what you want to do :)

Instead, I'd use one of the two options below.

If you're happy to just copy the UstMenuler reference from the menu parameter into the new view model instance, you can do that really easily:

select new MenuViewModel { UseMenuler = menu }

Or if you want to create a new UstMenuler and copy properties from menu:

select new MenuViewModel
{
    UstMenuler = new UstMenuler
    {
        ID = menu.ID,
        Name = menu.Name,
        YetkiID = menu.YetkiID,
        ControllerName = menu.ControllerName,
        ActionName = menu.ActionName
    }
}

Either way, you don't need your menuler variable which you appear not to be using anywhere else.

As it sounds like you're using LINQ to Entities, it sounds like you may want to separate the database query part from the projection part. The simplest way to do that in your case is to avoid the query expression syntax, so you can call AsEnumerable easily. So for example:

var query = dbContext.UstMenuler
    .Where(menu => menu.YetkiID == sessionYetkiID)
    .AsEnumerable() // Do the projection in-process
    .Select(menu => new MenuViewModel
                    {
                        UstMenuler = new UstMenuler
                        {
                            ID = menu.ID,
                            Name = menu.Name,
                            YetkiID = menu.YetkiID,
                            ControllerName = menu.ControllerName,
                            ActionName = menu.ActionName
                        }
                    })
    .ToList();

Upvotes: 9

Related Questions