Tanvi Khade
Tanvi Khade

Reputation: 1

Equivalent of LISTAGG in LINQ doesn't work

I have a SQL query

SELECT  
    M.EMPLOYEEID, LISTAGG(L.NAME, ',') WITHIN GROUP (ORDER BY L.NAME) AS Locations
FROM
    EMPLOYEEOFFICES M 
LEFT JOIN 
    OFFICELIST L ON M.OFFICELISTID = l.officelistid 
GROUP BY 
    M.EMPLOYEEID

The Linq equivalent I am trying to run:

var empOfficeLocations = (from om in _dbContext.EMPLOYEEOFFICES 
                          join ol in _dbContext.Officelists
                                  on om.Officelistid equals ol.Officelistid into grps
                          from grp in grps
                          group grp by om.Employeeid into g
                          select new
                                 {
                                     EmployeeId = g.Key,
                                     Locations = string.Join(",", g.Select(x => x.Name))
                                 }).ToList();

I have tried multiple versions of the above LINQ but haven't had any luck.

The error I get:

Processing of the LINQ expression 'GroupByShaperExpression:
KeySelector: e.EMPLOYEEID,
ElementSelector:ProjectionBindingExpression: EmptyProjectionMember
by 'RelationalProjectionBindingExpressionVisitor' failed. This may indicate either a bug or a limitation in Entity Framework. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.

The translation:

Compiling query expression:
      'DbSet<EMPLOYEEOFFICES>()
          .Join(
              inner: DbSet<Officelist>(),
              outerKeySelector: e => e.Officelistid,
              innerKeySelector: o => o.Officelistid,
              resultSelector: (e, o) => new {
                  e = e,
                  o = o
               })
          .GroupBy(
              keySelector: <>h__TransparentIdentifier0 => <>h__TransparentIdentifier0.e.Employeeid,
              elementSelector: <>h__TransparentIdentifier0 => <>h__TransparentIdentifier0.o.Name)
          .Select(g => new {
              EMPLOYEEID = g.Key,
              Locations = string.Join(
                  separator: ", ",
                  values: g)
           })

The result I want is simple:

EmployeeId Locations
emp1 loc1,loc2
emp2 loc1,loc3,loc4

I have searched Stackoverflow, tried chat gpt; the in-memory version seems to work, but try the same code with a db Context and everything falls apart.

The database in use is an Oracle database, version 11g

Microsoft.EntityFrameworkCore v5.0.9 Oracle.EntityFrameworkCore v5.21.4

How can I get this working? I do not wish to do this in-memory.

Upvotes: 0

Views: 93

Answers (0)

Related Questions