我创建了一个类,其中包含三个类作为属性:
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/