Sorry .. just to confirm..
Are you saying "object" is the only way to return my Anonymous Types List ?
Main Topics
Browse All TopicsHi Experts.
Need a piece of advice here please.
What's the best way to return the Anonymous type List ?
Lets say I have this list:
var usersList = (from u in usersTable.GetAll()
select new
{
Id = u.Id,
Login = u.UserLoginId,
Name = u.Name
}).ToList();
What would be the return type for the method that returns usersList ?
Yes, I could just return "object" but I would like to avoid boxing if possible as its not very memory sufficient.
Thank you in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I"m actually well aware of that method stated by you and Poke ( thanks to you Fernando for educating me in previous posts ). I guess I was curious to find out if there is a way to return the anonymous type without doing extra work of creating that extra anonymous type class:
public class empInfo
{
public int ID { get; set; }
public String Title { get; set; }
public String Name { get; set; }
}
Hi techsuppoprt;
The class empInfo that gets created by the query in my code snippet is not an anonymous type class but a concrete class that can be passed around the program without any problem.
If you were to execute this code:
// The only way to return an Anonymous type from a function
// is by an object as in this method
public object GetEmployeeInfo()
{
NorthwindDataContext nw = new NorthwindDataContext();
// employees is an Anonymous type
var employees = from emp in nw.Employees
select new
{
ID = emp.EmployeeID,
Title = emp.Title,
Name = emp.LastName + ", " + emp.FirstName
};
// Return an Anonymous type to the caller
return employees;
}
private void button2_Click(object sender, EventArgs e)
{
// Anonymous type returned from function
object employees = GetEmployeeInfo();
// This statement will only print to the console windoe/output window
// the SQL statement that was executed on the server. To get access to
// the data in the object you will need to use .Net Reflection class
// to determine what is in the object and its values.
Console.WriteLine(employee
}
Fernando
Business Accounts
Answer for Membership
by: FernandoSotoPosted on 2008-12-19 at 14:26:42ID: 23216042
Hi techsuppoprt;
To your question, "What would be the return type for the method that returns usersList ?", A List of an anonymous type. Anonymous types are given a name at compile time that can not be used in your code.
Fernando