简体   繁体   中英

How to make this linq expression?

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?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM