简体   繁体   中英

Using values of a custom object

I have the following as part of my entity framework class:-

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

Inside the inner .Select I have five assignments operations, but since some assignments depend upon the other so how I can write something such as:-

Ar = SN.SelectMany(…….)

So in this way I will only query the database only one time to retrieve the cd.SiteDefenitions??

One option would be to do it like this:

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;

I know it may not be quite what you were looking for, but you're not going to be able to get at the SN property during the parameterless constructor operation like that (AFAIK).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM