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

我有以下课程:

public class CommentList 
{
    string ItemType;
    string Comment1;
    string Status1;
    string DiscussionBoardId;
    Guid CourseId;
    Guid CommentID;
}

我正在尝试执行以下 LINQ 语句:

List<CommentList> query=
    from c in db.Comments
    join s in db.Status on c.StatusId equals s.StatusId
    join d in db.DiscussionBoards 
        on c.DiscussionBoardId equals d.DiscussionBoardId
    where d.CourseId=="CourseID"
    orderby d.ItemType, d.DiscussionBoardId
    select new CommentList {
        d.ItemType,
        c.Comment1,
        s.Status1,
        c.DiscussionBoardId,
        d.CourseId,
        c.CommentID
    };

问题是,编辑器抱怨 select 语句的第一个括号。就是说:

无法使用集合初始化程序实现类型“CommentList”,因为它没有实现“System.Collections.IEnumerable”。

有人可以帮助我并告诉我我做错了什么吗?

【问题讨论】:

    标签: c# asp.net linq collections


    【解决方案1】:

    将您的字段设为public,因为它们现在无法访问。

    public class CommentList
    {
      public string ItemType;
      public string Comment1;
      public string Status1;
      public string DiscussionBoardId;
      public Guid CourseId;
      public Guid CommentID;
    }
    

    并明确地将它们设置在您的初始化程序中。

    select new CommentList
    {
        ItemType = d.ItemType,
        Comment1 = c.Comment1,
        Status1 = s.Status1,
        DiscussionBoardId = c.DiscussionBoardId,
        CourseId = d.CourseId,
        CommentID = c.CommentID
    };
    

    【讨论】:

    • 很好,杰弗里!在我将属性设置为公共之前,它不起作用。谢谢!
    【解决方案2】:

    只是对我的评论进行更全面的跟进。您要做的是use object initialization,它要求您命名属性。它认为你正在尝试的是collection initialization

    List<CommentList> query = from c in db.Comments
                    join s in db.Status on c.StatusId equals s.StatusId
                    join d in db.DiscussionBoards on c.DiscussionBoardId equals d.DiscussionBoardId
                    where d.CourseId == "CourseID"
                    orderby d.ItemType, d.DiscussionBoardId
                    select new CommentList
                    {
                        ItemType = d.ItemType,
                        Comment1 = c.Comment1,
                        Status1= s.Status1,
                        DiscussionBoardId = c.DiscussionBoardId,
                        CourseId = d.CourseId,
                        CommentId = c.CommentID
                    };
    

    【讨论】:

    • 感谢您的链接,贾斯汀!
    • @JavaRox 我在集合初始化中添加了另一个,这就是它认为你正在尝试的内容
    • 这是我的问题。这很令人困惑,因为如果您经常使用匿名类型,它会警告您属性名称是多余的,然后您会回到固定类型并陷入其中。
    【解决方案3】:

    您只能在创建匿名类型时推断属性名称。

    您需要明确分配您的属性:

    new CommentList {
        ItemType = d.ItemType, 
        ...
    

    【讨论】:

      【解决方案4】:

      您的初始化不正确。您需要为显式属性分配值。

      select new CommentList
      {
          ItemType = d.ItemType,
          Comment1 = c.Comment1,
          Status1 = s.Status1,
          DiscussionBoardId = c.DiscussionBoardId,
          CourseId = d.CourseId,
          CommentID = c.CommentID
      };
      

      【讨论】:

      • 就是这样!我很感激!唷,看来我每天都在学习新东西!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多