如何使用 entity framework 的 Find 方法将 Array 作为参数传递来查找 List<Object>?

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

我想知道如何使用传递 Array(object[]) 作为参数的 Entity Framework 的 Find 方法找到 List<Object>

我想通过主键查找所有数据。

我首先填写一个列表,其中包含我将用作参考的所有 PK:

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

填写我的PK列表后,我尝试通过PK查找所有数据

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

当我尝试此操作时,出现以下错误:

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.

请与我们分享正确的做法。谢谢。

解决方案 如下所述,正确的解决方案是:

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

您正在寻找 Contains query:

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

这假定 PKId 是您的主 ID 列的名称(您没有指定名称)。