Kuke Kuke - 1 month ago 6
ASP.NET (C#) Question

How to return array from database in c#

I want to return my result as array list. My code looks like this:

public Person Get(string doctorCode)
{
using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())
{
return entities.Person.FirstOrDefault(e => e.DoctorLicenseNumber == doctorCode);
}
}


Some guy informed me that selected (all) result will be an array, so I tried this way, but im getting an error with select statement:

public IList<Person> Get(string doctorCode)
{
using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())
{
return entities.Person.Select<Person>(e => e.DoctorLicenseNumber == doctorCode);
}
}


any opinions?

Answer

Yeah sure you can! As a array:

public Person[] Get(string doctorCode)
{
    using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
    {
        return entities.Person.Where(e => e.DoctorLicenseNumber == doctorCode).ToArray();
    }
}

As a list:

public IEnumerable<Person> Get(string doctorCode)
{
    using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
    {
        return entities.Person.Where(e => e.DoctorLicenseNumber == doctorCode).ToList();
    }
}

not sure if it compiles, but you get the message :)