简体   繁体   中英

Entity Framework Code First Query

Here is my table(Company)

Companyid   Company_Name  ParentCompany_ID
957         Company1      6211
4816        Company2      Null
1922        Company3      7565
6211        Company4      Null

This is my Html.

               <div class="responsive-table">
                    <div class="row-width desktop-header">                           
                        <div class="col-md-5">Company</div>
                        <div class="col-md-5">Parent Company</div>
                    </div>
                    <div class="list-font row" data-ng-repeat="company in vm.companies">
                        <div class="col-md-2">
                            <div class="mobile-header">Name:</div>
                        </div>                            
                        <div class="col-md-5">
                            <div class="mobile-header">Parent Company:</div>
                        </div>
                    </div>
                </div>

I need to retrieve the Name and Parent Company name from the Company table. Here is my code.

private List<Company> GetCompany()
    {
        return dbContext.Companies
                .Where(p => p.Id == ID)                   
                .ToList();
    }

In the main function I have something like this

List<Company>company = GetCompany();
Result = new CompanyModel()
{ 
    Name = company.Name
};

I am kind of stuck with how to retrieve the Parent Company name.

Edit: The concept is that if the ParentCompany_Id is not null, then its the parent company for the companyid. For example, in the table, 957 is the child company and 6211 is the parentcompany. In the 2nd row, 4816 is the parent company since the ParentCompany_Id is null. So, I need to retrieve Company name and ParentCompany name from same table.

private Company GetCompany()
{
        return dbContext.Companies
                .Where(p => p.Id == ID)                   
                .FirstOrDefault();
}

and then,

string ParentCompanyId = GetCompany().ParentCompany_ID;

Your method GetCompany should be of Company type not List<Company> , since it return result by Id , also it's more convenient to have the id parameter to call it easily:

private static Company GetCompany(id)
 {
         if(id == null) return new Company();
         var company = dbContext.Companies
                                .SingleOrDefault(p => p.Id == id);
          return company;     
}

now you could create your model easily:

var company = GetCompany(957);
var result = new CompanyModel()
             { 
                Name = company.Name,
                ParName = GetCompany(company.ParentCompany_ID).Name
              };

Given your entities are likely going to include more details than just a name, including related data and the like...

public class Company
{
   public int CompanyId { get; set; }
   public string CompanyName { get; set; } 
   public int? ParentCompanyId { get; set; }
   [ForeignKey("ParentCompanyId")]
   public virtual Company ParentCompany { get; set; }
}

Then given retrieving a company by ID....

var company = dbContext.Companies.SingleOrDefault(c=> c.CompanyId == id);
// where company != null...

//lazy load the parent...
var parent = company.ParentCompany;

//eager load parent with company...
var company = dbContext.Companies
    .Where(c => c.CompanyId == id)
    .Include(c => c.Parent)
    .SingleOrDefault();

// better, if you know the view model you want...
var viewModel = dbContext.Companies
    .Where(c => c.CompanyId == id)
    .Select(c => new CompanyViewModel 
    { 
        c.CompanyId, 
        c.CompanyName, 
        ParentCompanyName = c.ParentCompany != null 
          ? c.ParentCompany.CompanyName 
          : string.Empty 
    }).SingleOrDefault();

That last example is from memory, may require some tweaking but the benefits of being explicit about the values retrieved is that it minimizes the data sent across the wire. For small domain objects, don't bother, but for larger, complex object graphs it can save a lot on performance.

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