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'.
Please provide me any solution or replacement to fix this issue.
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();