2010-07-08 93 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