none
rturn ToList RRS feed

  • Question

  • Hi, i have a method wic return a query.tolist(); i want to affet type of the method and set the result of thismethod in a datagrid

    i have the cod of query in a separate class:

     public ?????? getadresses()//this is theproblem
            {  
    Model1Container bd = new Model1Container();
                 ObjectQuery<Email_Account> emilaccouts = bd.Email_AccountJeu; 
    
                var query =
                from emilaccout in emilaccouts
                where emilaccout.black_list==false
                orderby emilaccout.ID
                select new { emilaccout.email_adress, emilaccout.name, emilaccout.surname, emilaccout.sexe, emilaccout.date_of_birth };
    
                return query.ToList();
    }
    
    
    cde beind:
     dgrecep2.ItemsSource=myclass.getadress();// it work with  dgrecep2.ItemsSource but i needto call method
    public ?????? getadresses()//this is theproblem

    Tuesday, May 15, 2012 12:51 PM

Answers

  • Hi DevDOTNETns;

    Because your query returns an anonymous type which gets created at compile time and is NOT guarantee to be a specific name that you can use in your code there is no way to know what to place in the return type of the function except that it is a list of some anonymous type. The best way to handle this is to create a class and have the Linq query create object of this type that it places in the List. See the sample code for an example.

    public List<EmailAccountInfo> getadresses()//this is theproblem
    {  
        Model1Container bd = new Model1Container();
        ObjectQuery<Email_Account> emilaccouts = bd.Email_AccountJeu; 
    
        var query =
            from emilaccout in emilaccouts
            where emilaccout.black_list==false
            orderby emilaccout.ID
            select new EmailAccountInfo
            { 
                Email_Adress = emilaccout.email_adress, 
                Name = emilaccout.name, 
                Surname = emilaccout.surname, 
                Sexe = emilaccout.sexe, 
                Date_of_Birth = emilaccout.date_of_birth 
            };
    
        return query.ToList();
    }
    
    public class EmailAccountInfo
    {
        public string Email_Adress { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Sexe { get; set; }
        public DateTime Date_of_Birth { get; set; }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, May 15, 2012 1:40 PM
  • Another option (if you're just passing it to a grid for displaying and don't need to access it as a strongly typed object at the caller side) is to return it just as an IList:

    public IList GetAdresses()
    {
      var query  = ....
      return query.ToIList();
    }

    Here's an extension method you can use to return a List<T> returned as an IList:

    public static IList ToIList(this IQueryable query)
    {
        return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(query.ElementType), query);
    }



     

       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Visual Studio add-in with loads of new features for the Entity Framework and Linq-to-SQL designers
     huagati.com/L2SProfiler - Runtime SQL query profiler for Linq-to-SQL and Entity Framework v4


    Thursday, May 17, 2012 1:56 AM
    Answerer

All replies

  • Hi DevDOTNETns;

    Because your query returns an anonymous type which gets created at compile time and is NOT guarantee to be a specific name that you can use in your code there is no way to know what to place in the return type of the function except that it is a list of some anonymous type. The best way to handle this is to create a class and have the Linq query create object of this type that it places in the List. See the sample code for an example.

    public List<EmailAccountInfo> getadresses()//this is theproblem
    {  
        Model1Container bd = new Model1Container();
        ObjectQuery<Email_Account> emilaccouts = bd.Email_AccountJeu; 
    
        var query =
            from emilaccout in emilaccouts
            where emilaccout.black_list==false
            orderby emilaccout.ID
            select new EmailAccountInfo
            { 
                Email_Adress = emilaccout.email_adress, 
                Name = emilaccout.name, 
                Surname = emilaccout.surname, 
                Sexe = emilaccout.sexe, 
                Date_of_Birth = emilaccout.date_of_birth 
            };
    
        return query.ToList();
    }
    
    public class EmailAccountInfo
    {
        public string Email_Adress { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Sexe { get; set; }
        public DateTime Date_of_Birth { get; set; }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, May 15, 2012 1:40 PM
  • Another option (if you're just passing it to a grid for displaying and don't need to access it as a strongly typed object at the caller side) is to return it just as an IList:

    public IList GetAdresses()
    {
      var query  = ....
      return query.ToIList();
    }

    Here's an extension method you can use to return a List<T> returned as an IList:

    public static IList ToIList(this IQueryable query)
    {
        return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(query.ElementType), query);
    }



     

       Cool tools for Linq-to-SQL and Entity Framework 4:
     huagati.com/dbmltools - Visual Studio add-in with loads of new features for the Entity Framework and Linq-to-SQL designers
     huagati.com/L2SProfiler - Runtime SQL query profiler for Linq-to-SQL and Entity Framework v4


    Thursday, May 17, 2012 1:56 AM
    Answerer