【问题标题】:Cannot initialize type "x" with a collection initializer because it does not implement 'System.Collections.IEnumerable'无法使用集合初始化程序初始化类型“x”,因为它没有实现“System.Collections.IEnumerable”
【发布时间】:2014-04-21 19:21:11
【问题描述】:

我正在尝试使用 linq to sql 和 ViewModel 连接 3 个表,将查询传递给 View 并显示结果,但它不起作用。

更新
DbContext 类:

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

视图模型:

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

控制器:

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);
    }

查看:

@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 />
   ...

}

我收到以下错误:

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc linq asp.net-mvc-4


    【解决方案1】:

    这没有多大意义。您正在创建视图模型的新实例MyViewModel,其中导航属性不是可枚举(!)并尝试加入这些?

    您不应该从您的数据库上下文的实例开始吗?我相信你应该这样做。

        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();
    

    【讨论】:

    • 谢谢!好的,现在我收到此错误:“无法使用集合初始化程序初始化类型'Proj.Models.MyViewModel',因为它没有实现'System.Collections.IEnumerable'”。
    • 您在MyViewModel 上调用了一个不带参数的构造函数,但随后您将使用一个包含 5 个成员的匿名对象对其进行初始化。我认为这行不通。
    【解决方案2】:

    问题是我必须在 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; }
    

    在 linq 查询中,我使用的是集合初始化程序而不是对象初始化程序:

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

    【讨论】:

      【解决方案3】:

      如果你写这个

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

      性能很差,你可以在你的 linq 代码中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多