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

标签 c# asp.net linq

我创建了一个类,其中包含三个类作为属性:

public class Feeds
{
    public Rentals Rentals { get; set; }
    public Agent Agents { get; set; }
    public NorthwindService.ServiceReference1.File File { get; set; }
}

我是这样使用它的:

var query = from r in ent.Rentals
            join a in ent.Agents on r.ListingAgentID equals a.AgentID
            select new Feeds
            {
                a.AgentID,
                a.Alias,
                a.Bio,
                a.Email,
                a.Fax,
                r.Firstname,
                r.IsStaff,
                r.Languages
            };

但是我得到了错误:

Cannot initialize type 'NorthwindService.WebForm1.Feeds' with a collection initializer because it does not implement 'System.Collections.IEnumerable' C:\Users\NorthwindService\NorthwindService\WebForm1.aspx.cs

请提出解决方案。

最佳答案

您在这里使用 C# 中的集合初始值设定项:

new myClass{a,b,c} 

其中 myClass 是一个集合,a,b,c 将被插入到这个集合中。

但是,您需要使用的符号是对象初始值设定项:

new myClass{
   myProperty1 = a,
   myProperty2 = b,
   myProperty3 = c
}

myClass 的成员将在这里被初始化。或者您可能需要使用经典构造函数,然后用括号更改括号:

new myClass(a,b,c)

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

相关文章:

c# - 如何在使用 ASP.NET Identity 确认帐户后执行自动登录

c# - C# WebBrowser Control 和我电脑上安装的 IE 有什么关系?

c# - 从 ASP.Net c# 中的 Gridview 控件中的文件夹获取文件名

c# - ListViewItem 不显示列上的实际文本

asp.net - 如何拒绝访问直接 URL 到我的部分 View ?

c# - 在 C# 中使用 LINQ 创建单个排列

C# Generic ToString Join if Array or IEnumerable

c# - 如何将 LINQ 左外连接限制为一行

c# - WPF应用程序遇到问题需要关闭,如何获得好的消息?

c# - 'generic' 电话号码的基本正则表达式