当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Linq Method To Get Data From Another Table Based On ID

问题描述:

I have a customer table, where I have the basic information name, address, postcode etc. A company can have multiple customers. So the relationship is loosely related to the Company table with the foreign key CompanyId. To search for customers I use, a IQueryable method as,

private IQueryable<CustomerModel> MapCustomer(IQueryable<Customer> query)

{

return (from a in query

select new CustomerModel

{

CompanyId = a.CompanyId,

CustomerId = a.Id,

CustomerName = a.Name,

AddressLine1 = a.AddressLine1,

PhoneNo = a.TelephoneNumber,

Postcode = a.PostCode,

ServiceCompletion = a.ServiceCompleteted,

ServiceDate = a.ServiceDate

});

}

As you can see, if I needed to search all I do is, call the function to return an IEnumerable, then use Where on it.

return MapCustomer(db.Customers.AsNoTracking().Where(x => x.Id == id)).FirstOrDefault();

The problem is I need to be able to populate the company name instead of the CompanyID. So when I get the result I would be able to see the Name of the company. What should I do to get this info? Any help is appreciated.

分享给朋友:
您可能感兴趣的文章:
随机阅读: