rich dvc rich dvc - 2 months ago 9
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

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 
                }).ToList();

Hope this helps!