Reputation: 681
I got a Database where I have data with an attribute like '12.34.56.78'.
To get this data I use
using (Data context = new Data(connection, compiledModel, true))
{
}
Now I want to only get the data where the first and second number is the same as my values.
I already tried this:
myData = context.Where(x => x.Numbers.Split('.')[0] == firstNumber && x.Numbers.Split('.')[1] == secondNumber)
But this only get me this error:
System.NotSupportedException: 'The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.'
How do I write this expression to get what I want?
Upvotes: 0
Views: 76
Reputation: 34189
When you write IQueryable
queries, you should always think about how it will be translated into an SQL query.
How would you do this with simple SQL query?
There is no Split
in SQL, so you will probably do something like:
SELECT * FROM Table WHERE Numbers LIKE '12.34.%'
This is an equivalent to the following LINQ:
string prefix = $"{firstNumber}.{secondNumber}.";
myData = context.Where(x => x.Numbers.StartsWith(prefix));
Upvotes: 1