How to find a List<Object> using Find method of entity framework passing Array as parameter?

試著忘記壹切 提交于 2019-12-02 13:33:33

问题


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:


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



来源:https://stackoverflow.com/questions/30304506/how-to-find-a-listobject-using-find-method-of-entity-framework-passing-array-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!