IEnumerable<T> to a List<T>

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.
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
Can yo not say:

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

Open in new window


And then....

return custs.ToList();

Open in new window

0
sbornstein2Author 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.
0
sbornstein2Author Commented:
If I pass it as an IEnumerable<Customer> it seems to work just fine.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Nitin SontakkeDeveloperCommented:
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.
0
sbornstein2Author 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.
0
sbornstein2Author Commented:
It is in a different namespace wondering if that is the issue
0
it_saigeDeveloperCommented:
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-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.