search object list in Entity [LINQ]

Asked

Viewed 149 times

1

I have a list of objects and would like to search 2 properties of that list in the database. with a list of primitive types I would do so:

(from n in db.Tabela
 where listaPrimitivos.Contains(n.propriedade)
 select n)

I thought how to search the list of objects in SQL:

SQL = "Select * from Tabela where 1!=1"
for(int i = 0; i < listaObjetos.Count; i++){
    SQL += " or (Tabela.prop1 = " + listaObjetos.prop1
    SQL += "     and Tabela.prop2 = " + listaObjetos.prop2 + ") "
}

How can I make this same query using the Entity Framework?

  • turn the two properties on your list into primitive type lists. :)

1 answer

1

See if it helps

var prop1Lista = listaPrimitivos.Select(x => x.Propriedade1).tolist();
var prop2Lista = listaPrimitivos.Select(x => x.Propriedade2).tolist();

var result = db.Tabela
         .Where(t=> prop1Lista 
                         .Any(lp=> lp == t.propriedade1)
                    && prop2Lista 
                         .Any(lp=> lp == t.propriedade2));
  • Conversion to VB: Dim query = db.TAB_FinanceiroTitulos.Where(Function(x) ListaNumero.Any(Function(y) x.NumeroControleOrigem.Equals(y.Numero) And x.Sequencia = y.Parcela))

  • 'Unable to create a Constant value of type 'Fn_conciliacaorecebimentovendasmarketplace+Titulob2wconsulta'. Only Primitive types or enumeration types are supported in this context'

  • heheh was what I thought the Entity Framework does not know how to send the object list to the database, you will have to create two same-type primitive lists for each property of your object.

  • Ma and how will I know that the Dice 0 of the first list is the Dice 0 of the second?

  • prq you need to know about index ? the first . Where(t=> will read the two lists for each element of the table .

Browser other questions tagged

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