Validating Entity Framework in Local List

I have a local list of values ​​that I need to verify the entity based on the database and return them.

If the list was already in the database, the following will work:

var list = /* some ef query */; var myList = context.Logs.Where(l => list.Any(li => l.LogNumber == li.LogNumber)); 

But if the list is local, it throws an error:

 var list = new List<Log>(); var myList = context.Logs.Where(l => list.Any(li => l.LogNumber == li.LogNumber)); 

Exception: Unable to process the type 'Data.Log[]', because it has no known mapping to the value layer.

So, how can I map a local list to a database list using EF?

+4
source share
1 answer

I have a different error than you have with the sample code, but I think the same idea. EF does not know how to translate a List<Log> into an SQL repository expression. It works when you are still in a request because it has not yet been serialized.

I understand that this is less than ideal, but I was able to make this request work by extracting the scalar values ​​of LogNumber and then using this in the request.

  var list = new List<Log>(); list.Add(new Log() { LogNumber = 1 }); var numbers = list.Select(l => l.LogNumber); var myList = m.Logs.Where(l => numbers.Contains(l.LogNumber)); 
+4
source

Source: https://habr.com/ru/post/1433861/


All Articles