Instruction Left Join in LINQ to entites

Asked

Viewed 221 times

0

I tried to do a left Join on LINQ as follows:

(from opr in db.Operacao
join vol in db.Volume on new { VOL_CODBAR = opr.OPR_CODBAR } equals new { VOL_CODBAR     = vol.VOL_CODBAR } into vol_join
from vol in vol_join.DefaultIfEmpty()
select new {
  opr.OPR_IDX,
  opr.OPR_CODEMP,
  opr.OPR_CODFIL,
  opr.OPR_NUMVEI,
  opr.OPR_TIPOPR,
  vol.VOL_TITLE
})

But at runtime, the exception is generated:

LINQ to Entities does not recognize the 'System.Collections.Generic.Ienumerable' method1[ImprotecSistemas.PackLocator.PackServer.DataAccess.vwObtemFuncionario] DefaultIfEmpty[Operacao](System.Collections.Generic.IEnumerable1[Improtecsistemas.PackLocator.Packserver.DataAccess.Operation])', which cannot be converted into a storage expression.

Below is what I want in SQL:

Select *
From operacao opr
     left join volumes vol on vol.VOL_CODBAR = OPR.OPR_CODBAR

I am using Entity Framework 3.5 C#

  • Have you tried so, with the equals without alias? Instead of new { VOL_CODBAR = opr.OPR_CODBAR } equals new { VOL_CODBAR = vol.VOL_CODBAR } only opr.OPR_CODBAR equals vol.VOL_CODBAR?

  • Yes. I have tried this way, and several others, but always error in function DefaultIfEmpty(). To make a palliative, I created a subselect, because at the moment I just need a column from the second table. But, this is not the correct.

  • researched and found that in fact DefaultIfEmpty() is not supported on Entity Framework 3.5.

  • Is there any alternative for him?

1 answer

1


Entity Framework 3.5 does not have LEFT JOIN for Linq queries. But there is another way to do this, described here: https://stackoverflow.com/a/5920182/2387977

I believe that in your case, the code should be something like this:

from opr in db.Operacao
select new 
{
  Operacao = opr,
  Volumes = opr.Volumes.Where(vol => vol.VOL_CODBAR = opr.OPR_CODBAR)
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.