asp.net-coreentity-framework-coreiqueryable.net-9.0ef-core-9.0

After migration to .NET 9, I have problem with `string.Join` inside Select statement


Previously, when I used .NET 8, I could use String.Join inside the Select statement for an IQueryable, as shown in this code:

var receiveTransactions = await repository.Context.Set<ReceiveTransaction>()
    .Select(x => new ThirdPartyTransactionReportModel
    {
        Date = x.Created,
        Customer = string.Join(" - ", x.CustomerIdentity.Customer.Name, x.CustomerIdentity.Customer.Type, x.CustomerIdentity.Customer.Nationality.Name),
    })
    .ToListAsync();

However, after upgrading to .NET 9, I am encountering an error:

Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan'.

Error

Please provide me any solution or replacement to fix this issue.


Solution

  • This is a known issue with several related mentions:

    To work around this, try specifying the array overload variant explicitly:

    var receiveTransactions = await repository.Context.Set<ReceiveTransaction>()
        .Select(x => new ThirdPartyTransactionReportModel
        {
            Date = x.Created,
            Customer = string.Join(" - ", new[] { x.CustomerIdentity.Customer.Name, x.CustomerIdentity.Customer.Type, x.CustomerIdentity.Customer.Nationality.Name }),
        })
        .ToListAsync();