Dan*_*Dan 1 c# entity-framework find entity-framework-6
我想知道如何找到一个List<Object>使用Find方法的实体框架传递Array(object[])作为参数?
我想通过Primary Key查找所有数据.
我首先用我将用作参考的所有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);
Run Code Online (Sandbox Code Playgroud)
在填写我的PK列表后,我尝试通过PK查找所有数据
var lServicosInformados = db.Servicos.Find(lCodigoServicos.ToArray());
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我收到以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
请与我们分享如何正确地做到这一点.谢谢.
解决方案 如下所述,正确的解决方案是:
var lServicosInformados = db.Servicos.Where(x => lCodigoServicos.Contains(x.PKId));
Run Code Online (Sandbox Code Playgroud)
您正在寻找一个Contains查询:
var lServicosInformados = db.Servicos.Where(x => lCodigoServicos.Contains(x.PKId));
Run Code Online (Sandbox Code Playgroud)
这假设PKId是您的主id列的名称(您没有指定名称).
| 归档时间: |
|
| 查看次数: |
1104 次 |
| 最近记录: |