使用自定义对象的值

时间:2013-07-09 20:19:04

标签: c# .net linq entity-framework

我将以下内容作为实体框架类的一部分: -

public   CustomerDetails GetCustomer(int id) {

        var customerDetails = entities.AccountDefinitions
       .Where(a => a.ORG_ID == id)
       .Select(cd => new CustomerDetails
       {
             AD = cd,
             SD = cd.SDOrganization,
             AA = cd.SDOrganization.AaaPostalAddresses,
             SN = cd.SiteDefinitions,
             Ar = cd.SiteDefinitions.SelectMany(a => a.DepartmentDefinitions.SelectMany(a2 => a2.SDUsers.Select(a3 => a3.AaaUser)))
       })
       .SingleOrDefault();
       return customerDetails;
        }

在内部。选择我有五个任务操作,但由于一些任务取决于另一个,所以我怎么写东西,如: -

Ar = SN.SelectMany(…….)

所以这样我只会查询数据库一次才能检索cd.SiteDefenitions吗?

1 个答案:

答案 0 :(得分:1)

一种选择就是这样做:

var customerDetails = entities.AccountDefinitions
    .Where(a => a.ORG_ID == id)
    .Select(cd => new CustomerDetails
    {
        AD = cd,
        SD = cd.SDOrganization,
        AA = cd.SDOrganization.AaaPostalAddresses,
        SN = cd.SiteDefinitions
   })
   .SingleOrDefault();

if (customerDetails != null)
{
    customerDetails.Ar = customerDetails.SN...
}

return customerDetails;

我知道它可能不是你想要的,但是在无参数构造函数操作(例如AFAIK)期间,你无法在get属性SN