We help IT Professionals succeed at work.

Convert DataTable to List

GeneBe
GeneBe asked
on
High Priority
48 Views
Last Modified: 2020-02-28
I have a working code but I would like to convert a block of code into a class a just call the classs. See code below.
DataTable results = dataAccess.Billing.GetDataTable(selectQuery);

                // Convert Table to List
                List<Service> serviceLines = new List<Service>();
                serviceLines = (from DataRow dr in results.Rows
                                select new Service()
                                {
                                    MemberNbr = dr["MEMBER_NBR"].ToString(),
                                    AffNbr = dr["AFF_NBR"].ToString(),
                                    AmtAllowP = Convert.ToDecimal(dr["AMTALLOW_P"]),
                                    AmtCharge = Convert.ToDecimal(dr["AMTCHARGE"])
                                }).ToList();

Can I remove this block of code and make a class or constant in another file so it is easy to maintain and just call it?
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
of course you can move the code around. Not sure to fully get your question!

Author

Commented:
I want to move this block of code somewhere else for easy maintenance and call it.
                                    MemberNbr = dr["MEMBER_NBR"].ToString(),
                                    AffNbr = dr["AFF_NBR"].ToString(),
                                    AmtAllowP = Convert.ToDecimal(dr["AMTALLOW_P"]),
                                    AmtCharge = Convert.ToDecimal(dr["AMTCHARGE"])
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:

Hi GeneBe;


You can't move this block of code

MemberNbr = dr["MEMBER_NBR"].ToString(),
AffNbr = dr["AFF_NBR"].ToString(),
AmtAllowP = Convert.ToDecimal(dr["AMTALLOW_P"]),
AmtCharge = Convert.ToDecimal(dr["AMTCHARGE"])

Without moving the rest of that block meaning you need to move all this

List<Service> serviceLines = new List<Service>();
serviceLines = (from DataRow dr in results.Rows
                          select new Service()
                          {
                              MemberNbr = dr["MEMBER_NBR"].ToString(),
                              AffNbr = dr["AFF_NBR"].ToString(),
                              AmtAllowP = Convert.ToDecimal(dr["AMTALLOW_P"]),
                              AmtCharge = Convert.ToDecimal(dr["AMTCHARGE"])
                          }).ToList();

What are you attempting to achieve?

Author

Commented:
That's what I thought but wasn't sure. I needed this advice.

Author

Commented:
That's what I thought but wasn't sure. I needed this advice.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
I haven't tried it but if you create a constructor  receiving a datarow in parameter, move the 4 rows to this new constructor, I am pretty sure that it would work.

Author

Commented:
I will try. thank you!