How to find a List using Find method of entity framework passing Array as parameter? c# entity-framework entity-framework-6 find

Question

I would like to know how to find a List<Object> using Find method of Entity Framework passing Array(object[]) as parameter?

I want to find all data by Primary Key.

I first fill a list with all PK that I will use as reference:

List<int> lCodigoServicos = new List<int>();
foreach (ServicosSelecionadosModelView servicoSelecionado in lServicos.FindAll(s => !string.IsNullOrEmpty(s.selecionado) && s.selecionado.ToLower() == "on" ))
         lCodigoServicos.Add(servicoSelecionado.servico.SerId);

After fill my list of PK, I try find all data by PK

var lServicosInformados = db.Servicos.Find(lCodigoServicos.ToArray());

When I try this, I get the following error:

The specified parameter type 'System.Int32[]' is not valid. Only scalar types, such as System.Int32, System.Decimal, System.DateTime, and System.Guid, are supported.

Please, share with us how to do it properly. Thanks.

Solution as described below, the right solution is:

var lServicosInformados = db.Servicos.Where(x => lCodigoServicos.Contains(x.PKId)); 
1
1
5/18/2015 1:45:10 PM

Accepted Answer

You are looking for a Contains query:

var lServicosInformados = db.Servicos.Where(x => lCodigoServicos.Contains(x.PKId)); 

This assumes PKId is the name of your primary id column (you didn't specify the name).

5
5/18/2015 1:31:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow