1

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 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @BrokenGlass. I was trying to use Find method, but, object[] is used for multiple keys of an entity, not multiple PKs. My mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.