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