我正在尝试使用以下代码通过另一个列表中的元素过滤列表:
public JsonResult GetEquipamentosByFiliaisInstalacao(List<EmpresaFocusDTO> filiais)
{
var instalacoesFiltradasPorFiliais = _db.Instalacao
.Where(i => filiais.Any(s => s.Id == i.IdFilialFocus))
.ToList()
我在linq行上遇到以下错误:
无法创建类型为'MasterCoin.DTO.Focus.EmpresaFocusDTO'的常量值。在这种情况下,仅支持基本类型或枚举类型。
我该怎么办?
这是@ er-shoaib要求的课程:
public class EmpresaFocusDTO
{
public int Id { get; set; }
public string Sigla { get; set; }
}
public partial class Instalacao
{
public int Id { get; set; }
public string Serie { get; set; }
public int Distancia { get; set; }
public int Deslocamento { get; set; }
public int IdEquipamentoFocus { get; set; }
public int IdClienteFocus { get; set; }
public bool Ativo { get; set; }
public string NomeEquipamentoFocus { get; set; }
public string NomeClienteFocus { get; set; }
public int IdFilialFocus { get; set; }
public int IdGrupoFocus { get; set; }
public string NomeFilialFocus { get; set; }
public string NomeGrupoFocus { get; set; }
public int IdRepresentanteFocus { get; set; }
public string NomeRepresentanteFocus { get; set; }
public string Uf { get; set; }
}
答案 0 :(得分:1)
错误提示:
仅允许使用基本类型
您需要创建仅id列的集合,然后在linq查询中使用它。 EF无法满足您的自定义类型集合。
尝试一下:
var ids = filiais.Select(s => s.Id).ToList();
var instalacoesFiltradasPorFiliais = _db.Instalacao
.Where(i => ids .Any(s => s == i.IdFilialFocus))
.ToList()