views:

36

answers:

1

Hi. Can i do something like that: I got some entity Customer with Id, Name, Comment

Now i want to get this entity from context with filled id and Name and Comment must be empty. I don't want to query it from database.

in T-SQL it simply:

Select Id, Name from Customers where id=4

Can i do that trick with Entity SQL something like that:

Select Customer.Id, Customer.Name from MyContext.Customer Where Customer.Id=4 
+1  A: 

If I understand your questions correctly you want to do this

from c in db.Customers where c.Id == 4 select {c.Id, c.Name};

this will only select the Id and Name properties from the Database

Edit

so like you mentioned in your comments you need something that selects into a new customer object, you really can't do this in a single statement. You can however do something like.

var selectedCustomers = (from c in MyContext.Customers where c.Id == 4 select {c.Id, c.Name};

foreach(Customer currentCustomer in selectedCustomer)
{
  Customer newCustomer = new Customer;
  newCustomer.Id = currentCustomer.Id;
  newCustomer.Name = currentCustomer.Name;
}
msarchet
Yes. I want something like that but you wrote a Linq query and I want Entity SQL queryI need strongly typed result with properties filled by my querySomething like this Select value row(Customer.Id, Customer.Name) from MyContext.Customer Where Customer.Id=4And result of this query must be Customer entity but not DbDataRecord
Brian J. Hakim
I mean this queries: context.CreateQuery<Customer>(...) Orcontext.ExecuteStoreQuery<Customer>(...)
Brian J. Hakim
@Brian: ESQL vs. LINQ has *zero* effect on the strong-typing of the result. @msarchet's LINQ *is* strongly-typed. It does not return `DbDataRecord` (have you tried it?). He's projecting onto an anonymous type, but you can also return POCOs. You cannot load a `Customer` half-way. It sounds to me like you want a DTO, not an entity. That's fine: Do it!
Craig Stuntz
@Craig Stuntz you beat me to it, @Brian J Hakim check my edit for what you can do.
msarchet
Thanks for yours explanations. I thought that in EF4.0 exists way to do it for one line of code. But now i got no doubts how it works.
Brian J. Hakim