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

Dot*_*row 43 c# linq asp.net

我创建了一个包含三个类作为属性的类:

public class Feeds
{
    public Rentals Rentals { get; set; }
    public Agent Agents { get; set; }
    public NorthwindService.ServiceReference1.File File { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

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
            };
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

无法使用集合初始值设定项初始化类型'NorthwindService.WebForm1.Feeds',因为它没有实现'System.Collections.IEnumerable'C:\ Users\NorthwindService\NorthwindService\WebForm1.aspx.cs

请建议解决方案

rem*_*rel 58

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

new myClass{a,b,c} 
Run Code Online (Sandbox Code Playgroud)

其中myClass是一个集合,a,b,c将插入此集合中

您需要使用的表示法是对象初始值设定项

new myClass{
   myProperty1 = a,
   myProperty2 = b,
   myProperty3 = c
}
Run Code Online (Sandbox Code Playgroud)

将初始化myClass的成员(或者您可能需要使用经典构造函数,然后使用括号更改括号)

new myClass(a,b,c)
Run Code Online (Sandbox Code Playgroud)

  • 我认为DotnetSparrow实际上是在尝试使用对象初始化器,但是在匿名类型的语法中,它将从源属性的名称推断出名称.指定类型时,这无效.在这种情况下,您还必须指定属性名称. (7认同)
  • 不确定这个答案是否适用于这个问题,但这是我的问题的解决方案. (4认同)

Pio*_*cik 42

应该:

var query = from r in ent.Rentals
           join a in ent.Agents on r.ListingAgentID equals a.AgentID
           select new Feeds
           {
                    Agents = a,
                    Rentals = r
           }
Run Code Online (Sandbox Code Playgroud)

  • 大声笑,我看不懂这个......:D有最简单的方法吗? (3认同)