IEnumerable<T> to a List<T>

sbornstein2
sbornstein2 used Ask the Experts™
on
Hello all,

I have the following class currently:
  [Serializable]
    [CollectionDataContract(Name = "Customers", ItemName = "Customer", Namespace = "")]
    public class Customers : List<Customer>
    { }

    [DataContract(Namespace = "")]
    public class Customer
    {
        [Key]
        [DataMember]
        public int CustomerID { get; set; }
        [DataMember]
        public string CustomerCode { get; set; }
        [DataMember]
        public string CustomerName { get; set; }
    }

Open in new window


I then have an IEnumerable coming back from the DapperORM tool that comes back as IEnumerable<Customer>.  How do I handle this I am getting issues everytime on casts etc?

That code sample is:
  public Customers  GetCustomers()
        {
            using (conn)
            {
                if (filters.Count() == 0)
                { 
                    return conn.GetList<Customer>(); //this needs to be changed to pass the Customers list.
                }
            }
        }

Open in new window


I can't change the dapper side it needs a <T> passed and returns an IEnumerable<Customer> in this case.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can yo not say:

IEnumerable<Customer> custs = conn.GetList<Customer>();

Open in new window


And then....

return custs.ToList();

Open in new window

Author

Commented:
Dapper passes back a generic IEnumerable it seems.   So the error for example when I do as you are saying shows:

Cannot implicitly convert type 'Systems.Collections.GenericList<Customer>' to 'Customers '.  An explicit conversion exists (are you missing a Cast).  

I have tried casting and it still does it all the time different similar errors.

Author

Commented:
If I pass it as an IEnumerable<Customer> it seems to work just fine.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

If it is not much of the problem change

public Customers  GetCustomers()

Open in new window

to

public List<Customer>  GetCustomers()

Open in new window

I personally don't see any reason why it should be a problem.

Author

Commented:
I guess the stupid question is then how do I get it to be a CollectionDataContract list as I am showing in the first post.   I need that serializable object.

Author

Commented:
It is in a different namespace wondering if that is the issue
Commented:
Just return the results of your base constructor that accepts an enumerable of customer; e.g. -
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;

namespace EE_Q29007176
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new Customers().GetCustomers();
            foreach (var customer in customers)
                Console.WriteLine(customer);
            Console.ReadLine();
        }
    }

    [Serializable, CollectionDataContract(Name = "Customers", ItemName = "Customer", Namespace = "")]
    public class Customers : List<Customer>
    {
        public Customers() : base() { ;}
        public Customers(int capacity) : base(capacity) { ;}
        public Customers(IEnumerable<Customer> collection) : base(collection) { ;}
        public Customers GetCustomers()
        {
            return new Customers(from i in Enumerable.Range(0, 15) select new Customer() { ID = i, Code = string.Format("Code{0}", i), Name = string.Format("Name{0}", i) });
        }
    }

    [DataContract(Namespace = "")]
    public class Customer
    {
        [Key, DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Code { get; set; }
        [DataMember]
        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format("{{ ID: {0}; Code: {1}; Name: {2} }}", ID, Code, Name);
        }
    }
}

Open in new window


Which produces the following output -Capture.PNG
So in your case your method would look like:
public Customers  GetCustomers()
{
    using (conn)
    {
        if (filters.Count() == 0)
        { 
            return new Customers(conn.GetList<Customer>()); //this needs to be changed to pass the Customers list.
        }
    }
}

Open in new window


Just make sure that you have the constructors defined as I did in my example.

-saige-

Author

Commented:
You are a life saver...:).  Thanks so much that was perfect.  Now I just need to figure out how to get rid of the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" in the header of the XML.   Thanks so much appreciate it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial