gpt4 book ai didi

c# - 无法使用集合初始值设定项实现类型 XYZ,因为它不实现 'System.Collections.IEnumerable'

转载 作者:可可西里 更新时间:2023-11-01 09:17:06 25 4
gpt4 key购买 nike

我有以下类(class):

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 语句的第一个括号上提示。它说:

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

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

最佳答案

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

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

关于c# - 无法使用集合初始值设定项实现类型 XYZ,因为它不实现 'System.Collections.IEnumerable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com