Entity SQL Anonymous type?

209 views Asked by At

I have a database with 2 tables :

  1. Personal : ID (PK), Name, Code (FK), Birthdate
  2. Social : Code (PK), Address

When I add a ADO.NET Entity Data Model name Model to my project. The Model.Context.tt has only Personals and Socials Dbset<>, so i can only choose 2 tables by using query :

string sql = "SELECT VALUE p FROM SqlEntities.Personals AS p"

SqlEntities is my entity name, so the thing i want is how to make a column in table become the Dbset<>, maybe I don't understand much but how i can use the anonymous type like :

stringsql = "SELECT p.ID, p.Name FROM SqlEntities.Personals AS p"

Model.Context.tt

1

There are 1 answers

3
Martin Staufcik On

With projection, it is possible to make the query return only some columns from one table, or to return selected columns from multiple tables joined together. Projection is done with the Select() method and supports anonymous types:

db.Personals.Select(x => new { ID = x.ID, Name = x.Name });