2010-07-08 105 views
0

我可以這樣做:我得到了一些實體客戶身份證,姓名,評論我可以通過實體SQL獲取實體嗎?

現在我想從充滿身份證的上下文中獲得此實體,並且名稱和評論必須爲空。我不想從數據庫中查詢它。

在T-SQL它只是

Select Id, Name from Customers where id=4 

我能做到這招與實體SQL類似的東西:

Select Customer.Id, Customer.Name from MyContext.Customer Where Customer.Id=4 

回答

1

如果我正確理解你的問題,你想這樣做

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

這隻會選擇D的IdName屬性atabase

編輯

所以就像你在你的評論中提到,你需要的東西,選擇到一個新的客戶對象,你真的不能在一個語句做到這一點。但是,你可以做類似的事情。

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; 
} 
+0

是的。我想要類似的東西,但是你寫了一個Linq查詢並且我想要實體SQL查詢 我需要強類型結果,其屬性由我的查詢填充 類似這樣從MyContext中選擇值行(Customer.Id,Customer.Name)。客戶在哪裏Customer.Id = 4 而這個查詢的結果必然是客戶的實體,但不是DbDataRecord – 2010-07-08 03:54:57

+0

我的意思是這個查詢: context.CreateQuery (...)或者 context.ExecuteStoreQuery (...) – 2010-07-08 03:57:36

+0

@ Brian:ESQL與LINQ對結果的強類型有*零*影響。 @ msarchet的LINQ * *是強類型的。它不會返回'DbDataRecord'(你有沒有試過?)。他投射到匿名類型,但您也可以返回POCO。您無法在中途加載「客戶」。這聽起來像你想要一個DTO,而不是一個實體。沒關係:做吧! – 2010-07-08 12:48:54

相關問題