如何找到一个List<对象>通过使用数组作为参数实体框架的Find方法? [英] How to find a List<Object> using Find method of entity framework passing Array as parameter?
问题描述
我想知道如何找到一个列表<对象>
使用实体框架的Find方法传球达阵( [对象]
)作为参数?
我要找到主键的所有数据。
我第一次填充,我将作为参考使用所有PK的列表:
列表与LT; INT> lCodigoServicos =新的List< INT>();
的foreach(ServicosSelecionadosModelView servicoSelecionado在lServicos.FindAll(S =>!string.IsNullOrEmpty(s.selecionado)及&放大器; s.selecionado.ToLower()==上))
lCodigoServicos。添加(servicoSelecionado.servico.SerId);
填写后我PK的名单,我试图通过PK
$ B $找出所有数据b VAR lServicosInformados = db.Servicos.Find(lCodigoServicos.ToArray());
当我尝试,我得到了以下错误:
指定的参数类型'System.Int32 []'是无效的。仅标量类型,如System.Int32,System.Decimal,System.DateTime的和的System.Guid,被支持。
请与我们分享如何做正确。谢谢
解决方案 $ B $如下所述B,正确的解决方案是:
VAR lServicosInformados = db.Servicos.Where(X => lCodigoServicos.Contains(x.PKId));
您正在寻找的 包含
查询:
VAR lServicosInformados = db.Servicos.Where(X => lCodigoServicos.Contains(x.PKId));
这假定 PKID
是名称您主ID柱(没有指定名称)。
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).
这篇关于如何找到一个List<对象>通过使用数组作为参数实体框架的Find方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!