代码之家  ›  专栏  ›  技术社区  ›  Brian J. Hakim

我可以通过实体SQL来填充实体吗?

  •  0
  • Brian J. Hakim  · 技术社区  · 16 年前

    我可以这样做吗:我找到了一些实体客户,他们的ID、姓名、评论

    现在,我想从具有填充ID的上下文中获取这个实体,并且名称和注释必须为空。我不想从数据库查询它。

    在T-SQL中,它只是:

    Select Id, Name from Customers where id=4
    

    我能用实体SQL这样的技巧吗?

    Select Customer.Id, Customer.Name from MyContext.Customer Where Customer.Id=4 
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   msarchet    16 年前

    如果我正确理解你的问题,你想这样做

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

    这将只选择 Id Name 数据库中的属性

    编辑

    所以,正如您在评论中提到的,您需要一些选择到新客户对象中的内容,您真的不能在单个语句中这样做。但是你可以做类似的事情。

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