I'm trying to replicate the below SQL query in LINQ Lambda by using String.Join. Could someone please point out how I can correct Lambda query.
I have formatted my SQL and placed the error message here.
Here is the error I receive:
System.InvalidOperationException: The LINQ expression 'DbSet<TblObligor>()
.Join(
inner: DbSet<TblObligorGuaranties>(),
outerKeySelector: t => t.ObligorId,
innerKeySelector: t0 => (decimal)t0.ObligorID,
resultSelector: (t, t0) => new TransparentIdentifier<TblObligor, TblObligorGuaranties>(
Outer = t,
Inner = t0
))
.Join(
inner: DbSet<TblObligorGuarantyTypes>(),
outerKeySelector: ti => ObligorService.MapObligorGuaranties(
o: ti.Outer,
og: ti.Inner).GuarantyTypeID,
innerKeySelector: t1 => (Nullable<int>)t1.GuarantyTypeID,
resultSelector: (ti, t1) => new TransparentIdentifier<TransparentIdentifier<TblObligor, TblObligorGuaranties>, TblObligorGuarantyTypes>(
Outer = ti,
Inner = t1
))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
I want a string concatenated colmn of GuarantyTypeDescription.
select
o2.ObligorID,
STUFF(
(
select
',' + cast(
ogt.GuarantyTypeDescription as nvarchar
)
from
tblObligor o
left join tblObligorGuaranties og on o.ObligorId = og.ObligorID
left join tblObligorGuarantyTypes ogt on og.GuarantyTypeID = ogt.GuarantyTypeID
where
1 = 1
and o.ObligorID = o2.ObligorID
and o.assetid = 1996323923 for xml path('')
),
1,
1,
''
) as xmlstring
from
tblObligor o2
where
1 = 1
and o2.assetid = 1996323923
Here is my code:
public async Task<IEnumerable<ObligorGuarantyDTO>> GetObligorsListAsync(int? assetId)
{
var obligorGuarantiesList = _context.TblObligor
.Join(_context.TblObligorGuaranties, o => o.ObligorId, og => og.ObligorID, (o, og) => new { o, og })
.Select(join => MapObligorGuaranties(join.o, join.og))
.Join(_context.TblObligorGuarantyTypes, og => og.GuarantyTypeID, ogt => ogt.GuarantyTypeID, (og, ogt) => new { og, ogt })
.Select(join => MapObligorGuarantyTypes(join.og, join.ogt))
.AsEnumerable();
return obligorGuarantiesList;
}
Here are my maps:
private static ObligorGuarantyDTO MapObligorGuaranties(TblObligor o, TblObligorGuaranties og)
=> new ObligorGuarantyDTO()
{
ObligorID = o.ObligorId,
GuarantyID = og.GuarantyID,
GuarantyTypeID = og.GuarantyTypeID,
Release = og.Release,
ReleaseDate= og.ReleaseDate,
Note= og.Note,
EditBy = og.EditBy,
EditTime= og.EditTime
};
private static ObligorGuarantyDTO MapObligorGuarantyTypes(ObligorGuarantyDTO og, TblObligorGuarantyTypes ogt)
=> new ObligorGuarantyDTO()
{
GuarantyTypeID = ogt.GuarantyTypeID,
GuarantyTypeDescription = String.Join(", ", ogt.GuarantyTypeDescription)
};
MapObligorGuarantiesandMapObligorGuarantyTypeshave no SQL server side equivalents, so entity framework cannot translate your query into SQL. Try to rewrite the lead part of your query without using custom C# functions, or write it so the they are applied only after the.AsEnumerable(). Note that only the portion of the query up to the.AsEnumerable()will be performed on the SQL server side and everything after will be performed in memory on the C# side, so be sure to include any data limiting.Where()conditions before the.AsEnumerable().