none
Anonymous Types in Functions RRS feed

  • Question

  • I have a web service method where I am getting two columns of info from the database via LINQ and EF. Note that I only want two columns because it's a drop down list that I need populated and not all the columns. Right before leaving the method, I have verified that the var customer object is populated properly with the data.

    Upon returning to the client method that called the web service method, the cusomter object is null. Evidently, the object seems not to be getting serialized properly on the transfer.

    The question is in order for me to just select "n" columns of info instead of all the columns, I need to perform the LINQ query with an anonymous (var) object.

    How can I successfully pass the object back from my web service method? Below, are my pertinent methods. The below method is the one that is giving me trouble.

    public IEnumerable<Customer> GetCustomerDropDownList()
            {
                YeagerTechEntities DbContext = new YeagerTechEntities();
     
                DbContext.Configuration.ProxyCreationEnabled = false;
     
                var customer = from c in DbContext.Customers
                               where c.CustomerID > 0
                               select new
                               {
                                   c.CustomerID,
                                   c.Email
                               };
     
                CloseConnection(DbContext);
     
                return customer as IEnumerable<Customer>;
            }
    If, however, I modify the web serive method to the following, the data gets serialized properly and the customer object makes it 
    across fine.
    public IEnumerable<Customer> GetCustomerDropDownList()
            {
                YeagerTechEntities DbContext = new YeagerTechEntities();
     
                DbContext.Configuration.ProxyCreationEnabled = false;
     
    
    IQueryable<Customer> customer = DbContext.Customers.Where(p => p.CustomerID > 0);
    
                CloseConnection(DbContext);             return customer;        }
    The interface for the web service method is defined as follows:
            [OperationContract]
            IEnumerable<Customer> GetCustomerDropDownList();
    
    The class generated by EF via a template is as follows:
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.ServiceModel;
     
    namespace YeagerTechModel
    {
        [Serializable]
        [DataContract(IsReference = true)]
        public partial class Customer
        {
            public Customer()
            {
                this.Projects = new HashSet<Project>();
            }
        
         [DataMember]
            public short CustomerID { getset; }
         [DataMember]
            public string Email { getset; }
         [DataMember]
            public string Company { getset; }
         [DataMember]
            public string FirstName { getset; }
         [DataMember]
            public string LastName { getset; }
         [DataMember]
            public string Address1 { getset; }
         [DataMember]
            public string Address2 { getset; }
         [DataMember]
            public string City { getset; }
         [DataMember]
            public string State { getset; }
         [DataMember]
            public string Zip { getset; }
         [DataMember]
            public string HomePhone { getset; }
         [DataMember]
            public string CellPhone { getset; }
         [DataMember]
            public string Website { getset; }
         [DataMember]
            public string IMAddress { getset; }
         [DataMember]
            public System.DateTime CreatedDate { getset; }
         [DataMember]
            public Nullable<System.DateTime> UpdatedDate { getset; }
        
         [DataMember]
            public virtual ICollection<Project> Projects { getset; }
        }
        
    }
    

    Bill Yeager
    Wednesday, January 18, 2012 1:08 AM

Answers

  • The problem is that your anonymous type is not a customer - it's a compiler generated class that contains two of the properties of a customer.

     

    If you actually need a "Customer" instance with only those properties filled in, you'd need to create this by converting your anonymous class into an actual Customer.  Something like:

    return customer.AsEnumerable().Select(c => new Customer {CustomerID = c.CustomerID, Email = c.Email});
    


    That being said, I wouldn't recommend this - I'd just return the customers (with their properties set) from your query, or make a custom (named) class to hold this information, and return instances of that class.  Returning Customers from a method that aren't "complete" seems like a maintenance nightmare.

     

    (In this case, I'd just return customers - like your working copy, but that's just me...)

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, January 18, 2012 2:15 AM