Skip to main content
GameDev.net gamedev.net
🔒 Locked

Convert Object to List<Type>

Started by Forward Dec 16, 2011 at 10:43 PM 2 replies 1.4k views
Original Post
Forward
Forward
I have this code and i want to convert the object to List
How can i do that instead of creating a new class with every items?


List<Riders> list_team_riders = new List<Riders>();


object obj = (from rider in frm.list_riders
join rcont in frm.list_ridercontract on rider.PlayerID equals rcont.riderID
where rcont.TeamID == tm.TeamID
select new { rider }).ToList();
ApochPiQ
ApochPiQ
Sounds like you want to use generics, although I'm honestly a little fuzzy on what exactly you're asking :-)
Nypyren
Nypyren
change:

select new { rider } // this syntax makes an "anonymous type", which I don't see any reason for.

to:

select rider



and change:

object obj

to:

var obj // this syntax infers the correct type from the query expression and should be List once you change the select clause.



After you've done that, assuming your old code copies the result into list_team_riders, you can just say:

var list_team_riders = (that big query expression);
Forward
Forward
Thanks Nypyren
It worked....so easy ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.