rich dvc rich dvc - 1 year ago 76
C# Question

populate List<myClass> lst from Entity Model using LinQ

I can do var lst = from p in myEntity.tblx --- no problem

instead I want to do something like

List<myClass> lst = from p in myEntity.tblx

where myClass has all the same fields as tblx

I have tried

List<myClass> lst = new List<myClass>();
lst = (from p in myEntity.tblx).ToList();

but that did not work.

say myClass looks like this

class myClass
int mainID {get; set;}
string fName {get; set;}
string lName {get; set;}

how can I populate

List<myClass> lst from myEntity.tblx

using Linq?

Answer Source

Did you try like below:

List<myClass> lst = (from p in myEntity.tblx
                select new myClass()
                    mainID = p.mainID,
                    fName = p.fname,
                    lName = p.lName 

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download