Home > Mobile >  Return from linq query
Return from linq query

Time:12-29

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

CodePudding user response:

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;
        }

CodePudding user response:

You need to update your method return type as tuple. like follows,

public (SewaPeralatan, SewaPeralatanDetail) FindByNotaSewa(string notaSewa)
{

}

And you should bit change inside method like this,

 var dataSewa = from sp in _context.SewaPeralatan
  join spd in _context.SewaPeralatanDetails
                           on sp.Id equals spd.SpId
                           where sp.NotaSewa == notaSewa
                           select (sp, spd);
                           
            return dataSewa.FirstOrDefault();
  • Related