简体   繁体   中英

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

I am trying to join 3 tables using linq to sql and a ViewModel, pass the query to the View and display the results, but it doesn't work.

Update
DbContext Class:

public partial class DbEntities : DbContext
{
    public DbSet<Foo> Foos{ get; set; }
    public DbSet<Bar> Bars{ get; set; }
    public DbSet<Fubar> Fubars{ get; set; }
}

ViewModel:

public class MyViewModel
{
    public Foo Foo{ get; set; }
    public Bar Bar{ get; set; }
    public Fubar Fubar{ get; set; }
}

Controller:

public ActionResult Index(string searchString)
    {
      //var db = new MyViewModel();  //wrong
        var db = new DbEntities();

        var query = (from f in db.Foos
                     join b in db.Bars on f.IDFoo equals b.IDFoo
                     join fb in db.Fubars on b.IDBar equals fb.IDBar
                     select new MyViewModel { 
                                f.IDFoo,
                                f.NameFoo, 
                                f.CityFoo, 
                                b.NameBar, 
                                fb.NameFubar });


        if (!String.IsNullOrEmpty(searchString))
        {
            query = query.Where(x => x.NameFubar.Contains(searchString));
        }
        return View(query);
    }

View:

@model IEnumerable<Proj.Models.MyViewModel>

@using (Html.BeginForm())
    {
        <p>
            @Html.TextBox("SearchString")
            <input type="submit" class="no-button" value="Search" />
        </p>
    }

@foreach (var item in Model)
{
   <div class="title">@Html.DisplayFor(modelItem => item.NameFubar)</div><br />
   ...

}

I get the following error:

1. "Cannot initialize type 'Proj.Models.MyViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable' "

This doesn't make much sense. You are creating a new instance of your view model, MyViewModel where navigation properties are not enumerables (!) and try to join on these?

Shouldn't you rather start with an instance of your db context? I do believe you should.

    var db = new SomethingThatIsADbContext();

    var query = (from f in db.Foos
                 join b in db.Bars on f.IDFoo equals b.IDFoo
                 join fb in db.Fubars on b.IDBar equals fb.IDBar
                 select new MyViewModel { 
                            f.IDFoo,
                            f.NameFoo, 
                            f.CityFoo, 
                            b.NameBar, 
                            fb.NameFubar }).ToList();

The problem was I had to set proprieties not classes in MyViewModel:

public short IDFoo { get; set; }
public string NameFoo { get; set; }
public string CityFoo { get; set; }
public string NameBar { get; set; }
public string NameFubar { get; set; }

And in the linq query I was using the collection initializer not the object initializer:

select new MyViewModel {
       IDFoo = f.IDFoo,
       NameFoo = f.NameFoo,
       CityFoo = f.CityFoo,
       NameBar = b.NameBar,
       NameFubar = fb.NameFubar 
       };

if you write this

query = query.Where(x => x.NameFubar.Contains(searchString));

performance very bad, you can where in your linq code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM