Reputation: 351
This is the IQueryable object that I wrote for one of my APIs. It just join with some of the schemas and returns company data. (I am using .net 6 for the project)
the query which return IQueryable object
private IQueryable<CompanyDto> FetchAllComnpay()
{
return from com in _context.Companies
join cty in _context.Cities on com.CityId equals cty.Id
join con in _context.Countries on cty.CountryId equals con.Id
join lc in _context.Currencies on com.LocalCurrencyId equals lc.Id
into slc
from sublc in slc.DefaultIfEmpty()
join ic in _context.Currencies on com.InterNationalCurrencyId equals ic.Id
into sic
from subic in sic.DefaultIfEmpty()
select new CompanyDto
{
Id = com.Id,
CompanyName = com.CompanyName,
CompanyCode = com.CompanyCode,
CountryId = cty.CountryId,
CountryName = con.CountryName,
CityId = cty.Id,
CityName = cty.CityName,
MobileNo = com.MobileNo,
PhoneNo = com.PhoneNo,
Email = com.Email,
Web = com.Web,
ZipCode = com.ZipCode,
Address = com.Address,
CreatedBy = com.CreatedBy,
CreatedDate = com.CreatedDate,
UpdatedBy = com.UpdatedBy,
LastUpdatedDate = com.LastUpdatedDate ?? null,
LocalCurrencyId = com.LocalCurrencyId ?? 0,
LocalCurrencyName = sublc.CurrencyName ?? "",
InterNationalCurrencyId = com.InterNationalCurrencyId ?? 0,
InterNationalCurrencyName = subic.CurrencyName ?? "",
};
}
Then I fetch a list of companies like this
public async Task<IEnumerable<CompanyDto>> GetAllCompanies()
{
return await FetchAllComnpay().AsNoTracking().ToListAsync();
}
Then fetch a single company the like this
public async Task<CompanyDto> GetCompayByIdAsync(int id)
{
return await FetchAllComnpay().Where(e => e.Id == id).AsNoTracking().FirstAsync();
}
Actually, I am forced to work with .Net core in my org. That's why I am asking this.
Here's the point that I wanted to know regarding this context
Upvotes: 0
Views: 207
Reputation: 35037
- Am I on the right track
Yes, you are.
- If I m not what are the best available best practices that I can apply in this type of query?
- Will you kindly provide me with any suggestions, please?
Here we deal with specific issues (there is an error, it takes too long, etc.).
For "any suggestion" https://codereview.stackexchange.com/ will be better.
Upvotes: 1