C# 无法使用集合初始值设定项实现类型 XYZ,因为它没有实现“System.Collections.IEnumerable”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18494208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:23:56  来源:igfitidea点击:

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

c#asp.netlinqcollections

提问by Kevin

I have the following class:

我有以下课程:

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

I'm trying to do the following LINQ statement:

我正在尝试执行以下 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
    };

The problem is, the editor is complaining on the first parenthesis of the select statement. It's saying:

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

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

无法使用集合初始值设定项实现类型“CommentList”,因为它没有实现“System.Collections.IEnumerable”。

Can someone help me out and tell me what I'm doing wrong?

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

采纳答案by Khan

Make your fields publicbecause they are not accessible as they are now.

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

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

And explicity set them in your initializer.

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

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

回答by Claudio Redi

Your initialization is not correct. You need to assign values to explicit properties.

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

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

回答by SLaks

You can only infer property names when creating anonymous types.

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

You need to explicitly assign your properties:

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

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

回答by Justin Pihony

Just a fuller follow-up to my comment. What you are trying to do is use object initialization, which requires you to name the properties. What it thinks you are trying is 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
                };