I have a LINQ statement as follows:
var playedBanDataList =
from bannedPlayers in query
select new PlayerBanData
{
Admin = bannedPlayers.Admin,
BannedUntil = bannedPlayers.BannedUntil,
IsPermanentBan = bannedPlayers.IsPermanentBan,
PlayerName = bannedPlayers.PlayerName,
Reason = bannedPlayers.Reason,
IpAddresses = bannedPlayers.IpAddresses.Split(new [] {","}, StringSplitOptions.RemoveEmptyEntries).ToList()
};
return playedBanDataList.ToList();
This fails because split function fails on IpAddresses
as LINQ to Entities cannot translate this query to SQL.
This makes sense, but then what's an equivalent way of accomplishing this elegantly? The only way I've thought of is to manually run a loop on the retrieved string then splitting it, but I'd like to get it in one go.
You can use AsEnumerable
to make the select occur in memory instead of EF.
var playedBanDataList = query.AsEnumerable()
.Select(bannedPlayers => new PlayerBanData
{
Admin = bannedPlayers.Admin,
BannedUntil = bannedPlayers.BannedUntil,
IsPermanentBan = bannedPlayers.IsPermanentBan,
PlayerName = bannedPlayers.PlayerName,
Reason = bannedPlayers.Reason,
IpAddresses = bannedPlayers.IpAddresses.Split(
new [] {","},
StringSplitOptions.RemoveEmptyEntries).ToList()
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With