Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
C#
List<object> d = new List<object>();


      var q = (from t1 in Bank.Users join t2 in Bank.Questions on t1.Id equals t2.Id_Users select new { t1.Id, t1.Name }).ToList();




how to Convert Var To List?!!!
Thanks So Much
Posted
Comments
Christian Amado 27-Aug-12 14:47pm    
var is already a List! What error do you have?
AmitGajjar 27-Aug-12 22:13pm    
Why you have used List<object> if you know the type of generic then best practice is to use that. Here i think you can create your own class and use it like List<bank> instead of List<object>

Let's make it more readable than just keeping it in objects.
Have a class or a struct called UserInfo (lets' assume).
C#
public class UserInfo
{
     public int Id { get;set; }
     public string Name { get;set; }
}

Define the list/enumeration.
C#
List<UserInfo> d = new List<UserInfo>();

Slightly alter your query.
C#
var q = (from t1 in Bank.Users 
         join t2 in Bank.Questions 
         on t1.Id equals t2.Id_Users 
         select new UserInfo
         { 
           Id = t1.Id, 
           Name = t1.Name 
         });

Now, q is an IEnumerable<UserInfo>. You can convert it to list using .ToList() method.
C#
d = q.ToList();
 
Share this answer
 
v2
XML
List<object> d = (from t1 in Bank.Users join t2 in Bank.Questions on t1.Id equals t2.Id_Users select new { t1.Id, t1.Name }).ToList();
 
Share this answer
 
Comments
Seyed Ahmad Mirzaee 27-Aug-12 11:34am    
Error!!!
i dont Know how to resolve!!!
[no name] 27-Aug-12 11:37am    
"Error" does not tell anyone anything other than you got an error. Out of the thousands of errors that it could be should we guess?
Santhosh Kumar Jayaraman 28-Aug-12 0:55am    
What was the error?
try:

C#
List<object> d = new List<object>();
var q = (from t1 in Bank.Users join t2 in Bank.Questions on t1.Id equals t2.Id_Users select new { t1.Id, t1.Name });

d= q.SelectMany(x =>x).ToList();
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900