0

I'm new to EF and I'm having trouble doing the simplest thing...

public class Person : DTO
{
    public String ID { get; set; }
    public String Fname { get; set; }
    public String Lname{ get; set; }
}

I would like to use either IQueryable<Person> or ObjectReuslt<Person> to extract Fname from my object.

 peopleEntities entities = new peopleEntities();
 string queryStr = "select value c from peopleEntity.Person as c Where c.ID=" + personID;
 IQueryable<EntityObject> query = entities.CreateQuery<EntityObject>(queryStr);

I see that CreateQuery can both return IQueryable and ObjectResult. I'd like to know whats the lightest way that I can extract Fnames into a list from my query result.

lzc
  • 1,645
  • 5
  • 27
  • 41
  • Why are you writing sql queries when using EF? you lose all the strongly typed awesomeness that EF gets you – Kritner Jul 27 '15 at 13:48
  • @Kritner Well, entity table name is supposed to be dynamic and I thought formatting sql string was the easiest. What do u think? Create query will not let me do something like... `select fname from peopleEntity.Person ...` – lzc Jul 27 '15 at 13:51

1 Answers1

0

I'm interpreting your question as "Get the first name of the provided personId". Assuming your peopleEntities contains a DbSet<Person> Person you could do the following:

public string GetFirstNameForPeron(string personId)
{
    peopleEntities entities = new peopleEntities(); // new up your EF context

    return entities
        .Person // from the person entities
        .Where(w => w.ID == personId) // Where the ID = personId
        .FirstOrDefault(); // take the first available
        .Fname; // only the Fname property
}
Kritner
  • 13,557
  • 10
  • 46
  • 72
  • So what if the entity `Person` and field `Fname` are dynamic. Meaning I don't know which entity I am dealing with, and also don't know the field I want. – lzc Jul 27 '15 at 14:19
  • That sounds like a very different scenario that what you're asking about in your question. http://stackoverflow.com/questions/21084916/dynamically-select-columns-in-runtime-using-entity-framework – Kritner Jul 27 '15 at 14:23