Search code examples
c#linqasp.net-coreservicemodel

Return from linq query


How return linq query below this

public SewaPeralatan FindByNotaSewa(string notaSewa)
{
    var dataSewa = 
        from sp in _context.SewaPeralatan
        join spd in _context.SewaPeralatanDetails on sp.Id equals spd.SpId
        where sp.NotaSewa == notaSewa
        select new { sp, spd };
                           
    return dataSewa.FirstOrDefault();
}

the return get error like this

"Cannot implicitly convert type '<anonymous type: SYN.Models.SewaPeralatan sp, SYN.Models.SewaPeralatanDetail spd>' to 'SYN.Models.SewaPeralatan'"

What I expect from the return is to be able to get data from the SewaPeralatanDetail table The SewaPeralatanDetail table contains details from the SewaPeralatan Table


Solution

  • if you are using EF you can use linq like this to return SewaParlatan

     public SewaPeralatan FindByNotaSewa(string notaSewa)
            {
                var dataSewa =
                    _context.SewaPeralatan
                    .Where(b => b.NotaSewa == notaSewa)
                    .Include(b => b.SewaPeralatanDetails)
                    .FirstOrDefault();
                    
    
                return dataSewa;
            }