I have created a class which holds three classes as properties:
public class Feeds
{
public Rentals Rentals { get; set; }
public Agent Agents { get; set; }
public NorthwindService.ServiceReference1.File File { get; set; }
}
and I am using it like this:
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
};
but I am getting the error:
Cannot initialize type ‘NorthwindService.WebForm1.Feeds’ with a collection initializer because it does not implement ‘System.Collections.IEnumerable’ C:UsersNorthwindServiceNorthwindServiceWebForm1.aspx.cs
Please suggest a solution.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You are using here the collection initializer in C# :
new myClass{a,b,c}
where myClass is a collection, and a,b,c will be inserted into this collection.
But, the notation you need to use is the object initializer:
new myClass{
myProperty1 = a,
myProperty2 = b,
myProperty3 = c
}
where the member of a myClass will be initialized. Or maybe you need to use classic constructor and then change your bracket with parenthesis:
new myClass(a,b,c)
Method 2
Should be:
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
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0