Howto implement String.Split function via SQLiteFunctions
Posted: Thu 21 Mar 2019 04:31
Hello.
Let's say we have the following entity (I use EF6):
And last property is filled with comma separated direction names:
I want to select entites with Linq to Entites applying among others a filter by direction name:
The code above fails because Linq to Entites doesn't support System.String[] Split(char[]) method.
The only workaround for now is to switch from Linq to Entites to Linq to Objects via .AsEnumerable() method:
But that approach is ugly and slow (because .AsEnumerable() loads data from database to memory - and amount of that data is significantly more that I really need).
So is it possible to get the first query work (maybe with the hel of SQLiteFunctions class)?
P. S. If it will be easier to implement I can replace String Directions -> "East, North, South" with Int32 DirectionCodes -> "0, 2, 3" and filter by Int32 value.
Let's say we have the following entity (I use EF6):
Code: Select all
public class MyEntity
{
public Guid Id { get; set; }
public String Name { get; set; }
public Int32 Category { get; set; }
public String Directions { get; set; }
}
Code: Select all
MyEntity.Directions = "East, North, South";
Code: Select all
var result = MyDBContext.MyEntites.Where(e => (e.Category == 123) && (e.Directions.Split(',').Contains("East")).Tolist()
The only workaround for now is to switch from Linq to Entites to Linq to Objects via .AsEnumerable() method:
Code: Select all
var result = MyDBContext.MyEntites.Where(e => e.Category == 123).AsEnumerable().
Where(e => e.Directions.Split(',').Contains("East")).Tolist()
So is it possible to get the first query work (maybe with the hel of SQLiteFunctions class)?
P. S. If it will be easier to implement I can replace String Directions -> "East, North, South" with Int32 DirectionCodes -> "0, 2, 3" and filter by Int32 value.