C# 在Where子句中使用Contains会导致错误:';阵列索引';在LINQ to实体中不支持

C# 在Where子句中使用Contains会导致错误:';阵列索引';在LINQ to实体中不支持,c#,entity-framework,linq-to-entities,C#,Entity Framework,Linq To Entities,我想检查int[]数组的长度。如果没有 public List<Parent_Company> GetParentCompany(params int[] clientIds) { IEnumerable<Companie> ccs; if (clientIds.Length == 0) ccs = _xciRepository.GetAll<Companie>(); else if (clientIds.Length == 1)

我想检查
int[]
数组的长度。如果没有

public List<Parent_Company> GetParentCompany(params int[] clientIds)
{
  IEnumerable<Companie> ccs;
  if (clientIds.Length == 0)
     ccs = _xciRepository.GetAll<Companie>();
  else if (clientIds.Length == 1)
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientIds[0]);
  else
    ccs = _xciRepository.Find<Companie>(x => clientIds.Contains(x.CustCompID));

   //-- more codes here
 }
公共列表GetParentCompany(参数int[]clientId)
{
可数ccs;
if(clientId.Length==0)
ccs=xciRepository.GetAll();
else if(clientId.Length==1)
ccs=xciRepository.Find(x=>x.CustCompID==clientId[0]);
其他的
ccs=\uxcirepository.Find(x=>clientIds.Contains(x.CustCompID));
//--这里有更多代码
}
我收到以下错误:“LINQ to实体中不支持LINQ表达式节点类型'ArrayIndex'。

我怀疑是else中的语句引起了问题,Find方法中的表达式,即
x=>clientId.Contains(x.CustCompID)

有办法解决这个问题吗?否则,我在linq中多次使用Contains


感谢您的帮助。

最简单的方法就是删除这两行:

else if (clientIds.Length == 1)
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientIds[0]);
else if(clientId.Length==1)
ccs=xciRepository.Find(x=>x.CustCompID==clientId[0]);
您也可以这样做:

else if (clientIds.Length == 1)
{
     var clientId=clientIds[0];
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientId);
}
else if(clientId.Length==1)
{
var clientId=clientId[0];
ccs=xciRepository.Find(x=>x.CustCompID==clientId);
}

最简单的方法是删除这两行:

else if (clientIds.Length == 1)
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientIds[0]);
else if(clientId.Length==1)
ccs=xciRepository.Find(x=>x.CustCompID==clientId[0]);
您也可以这样做:

else if (clientIds.Length == 1)
{
     var clientId=clientIds[0];
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientId);
}
else if(clientId.Length==1)
{
var clientId=clientId[0];
ccs=xciRepository.Find(x=>x.CustCompID==clientId);
}

很可能有一种简单的方法可以解决您的问题-只需使用一个临时变量,您的代码如下所示:

public List<Parent_Company> GetParentCompany(params int[] clientIds)
{
    IEnumerable<Companie> ccs;
    if (clientIds.Length == 0)
        ccs = _xciRepository.GetAll<Companie>();
    else if (clientIds.Length == 1)
    {
        var clientId = clientIds[0];
        ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientId]);
    }
    else
        ccs = _xciRepository.Find<Companie>(x => clientIds.Contains(x.CustCompID));

    //-- more codes here
}
公共列表GetParentCompany(参数int[]clientId)
{
可数ccs;
if(clientId.Length==0)
ccs=xciRepository.GetAll();
else if(clientId.Length==1)
{
var clientId=clientId[0];
ccs=xciRepository.Find(x=>x.CustCompID==clientId]);
}
其他的
ccs=\uxcirepository.Find(x=>clientIds.Contains(x.CustCompID));
//--这里有更多代码
}

很可能有一种简单的方法可以解决您的问题-只需使用一个临时变量,您的代码如下所示:

public List<Parent_Company> GetParentCompany(params int[] clientIds)
{
    IEnumerable<Companie> ccs;
    if (clientIds.Length == 0)
        ccs = _xciRepository.GetAll<Companie>();
    else if (clientIds.Length == 1)
    {
        var clientId = clientIds[0];
        ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientId]);
    }
    else
        ccs = _xciRepository.Find<Companie>(x => clientIds.Contains(x.CustCompID));

    //-- more codes here
}
公共列表GetParentCompany(参数int[]clientId)
{
可数ccs;
if(clientId.Length==0)
ccs=xciRepository.GetAll();
else if(clientId.Length==1)
{
var clientId=clientId[0];
ccs=xciRepository.Find(x=>x.CustCompID==clientId]);
}
其他的
ccs=\uxcirepository.Find(x=>clientIds.Contains(x.CustCompID));
//--这里有更多代码
}

问题不在于
包含的
及其索引属性
clientId[0]
。我想问你为什么要使用
。查找
而不是更通用的
。Where
。Single
?为什么GetParentCompany不是
IQueryable
的扩展方法?@RobertMcKee,
Find
在这种情况下不是linq。在我之前的人在存储库中定义了此方法。我同意你的说法,它应该以不同的方式调用。问题不在于
包含
它的索引属性
clientId[0]
。我想问你为什么要使用
。查找
而不是更通用的
。Where
。Single
?为什么GetParentCompany不是
IQueryable
的扩展方法?@RobertMcKee,
Find
在这种情况下不是linq。在我之前的人在存储库中定义了此方法。我同意你的看法,它本应该有不同的称呼。