At the advice from people on here, I am trying to rewrite the linq query below using Navigation Properties.
I haven't gotten very far, because I'm not sure how to replace the joins in my Linq query with Navigation Properties.
So far I have this:
await _context.StarList(sl => new
{
sl.StarId,
sl.StarType,
sl.StarTitle,
sl.ChemicalId...
}
But then, in the original query, I start using 'chemicalatoms' in the query.
So I'm not sure how to transition to them.
So my question is, how do I replace the joins in my original query with Navigation Properties?
Thanks!
public async Task<ActionResult<object>> GetStarChemicalData(string starID)
{
var starChemicalData = await (from starlist in _context.StarList
join ql in _context.ChemicalList on starlist.ChemicalId equals ql.ChemicalId into stars
from chemicallist in stars.DefaultIfEmpty()
join qc in _context.ChemicalAtoms on chemicallist.ChemicalId equals qc.ChemicalId into chemicals
from chemicalatoms in chemicals.DefaultIfEmpty()
join nk in _context.StarLinks on chemicalatoms.AtomId equals nk.AtomId into links
from starlinks in links.DefaultIfEmpty()
where starlist.StarId == starID
select new
{
StarId = starlist.StarId,
StarType = starlist.StarType,
StarTitle = starlist.StarTitle,
ChemicalId = starlist.ChemicalId,
AtomId = (Guid?)chemicalatoms.AtomId,
OrderId = chemicalatoms.OrderId,
ChemicalText = chemicallist.ChemicalText,
AtomText = chemicalatoms.AtomText,
Wavelength = chemicalatoms.Wavelength,
isRedShifted = (starlinks.AtomId != null && starlist.StarType == 1) ? 1
: (starlinks.AtomId == null && starlist.StarType == 1) ? 0
: (int?)null
})
.GroupBy(x => x.StarId)
.Select(g => new
{
StarId = g.FirstOrDefault().StarId,
StarType = g.FirstOrDefault().StarType,
StarTitle = g.FirstOrDefault().StarTitle,
ChemicalId = g.FirstOrDefault().ChemicalId,
ChemicalText = g.FirstOrDefault().ChemicalText,
ChemicalAtoms = (g.FirstOrDefault().AtomId != null ? g.Select(x => new
{
AtomId = x.AtomId,
OrderId = x.OrderId,
AtomText = x.AtomText,
Feedback = x.Wavelength,
IsCorrect = x.isRedShifted
}) : null)
}).FirstOrDefaultAsync();
return starChemicalData;