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.

2
  • Why are you writing sql queries when using EF? you lose all the strongly typed awesomeness that EF gets you Commented Jul 27, 2015 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 ... Commented Jul 27, 2015 at 13:51

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
That sounds like a very different scenario that what you're asking about in your question. stackoverflow.com/questions/21084916/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.