Addorupdate - use array as parameter

Asked

Viewed 173 times

8

I’m doing tests with Entity Framework and I had a problem when placing a array as a parameter.

  1. Why when I assign the conversion of array in a function variable and directly in the method parameter does not work?

  2. This behavior is normal?

  3. If yes, what name?

Works

List<Product> product = new List<Product>()
var p = product.ToArray();
db.Product.AddOrUpdate(x => x.Id, p);

Doesn’t work

List<Product> product = new List<Product>()
db.Product.AddOrUpdate(x => x.Id, product.ToArray());
  • 3

    Doesn’t work like? It’s certainly not because of missing one ) in db.Product.AddOrUpdate(x => x.Id, product.ToArray(); or is?

  • This is not why. When it is converted into the parameter it just does not insert, it does not give any error.

  • You have how to explain how this variable product is formed?

  • @I edited the question.

  • But you don’t need to convert to array to use AddOrUpdate. And the list is empty. This is correct?

  • @Ciganomorrisonmendez the list comes through a Post, put only to identify that is a list of objects. Addorupdate expects a Product type array (params Product[]).

Show 1 more comment

1 answer

1

This is because LINQ to Entities requires the entire LINQ expression to be translated into a server query and therefore you cannot call a method from outside, in your case Linq does not reorganize the data within its methods, that is to say there is no way to convert your product to array product.ToArray() within the method AddOrUpdate this method expects an array and not an object calling the method ToArray() and return an array.

For this to work, you will need to restructure your query expression into an expression that Entity Framework can handle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.