Avatar of sbornstein2
sbornstein2
 asked on

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.
C#ASP.NET

Avatar of undefined
Last Comment
sbornstein2

8/22/2022 - Mon
Nitin Sontakke

Can yo not say:

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

Open in new window


And then....

return custs.ToList();

Open in new window

sbornstein2

ASKER
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.
sbornstein2

ASKER
If I pass it as an IEnumerable<Customer> it seems to work just fine.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Nitin Sontakke

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.
sbornstein2

ASKER
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.
sbornstein2

ASKER
It is in a different namespace wondering if that is the issue
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
it_saige

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sbornstein2

ASKER
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.