0

I'd like to check for the length of the the int[] array. If there is not

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
 }

I'm getting the following error: "The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities."

And I suspect it's the statement in the else that's causing the problem, the expression inside the Find method i.e.x => clientIds.Contains(x.CustCompID)

Is there a way to fix that? Otherwise, I've used Contains several times in linq.

Thanks for helping.

3
  • 1
    The problem isn't with the Contains its with the indexed property clientIds[0]. Commented Nov 24, 2015 at 18:03
  • 1
    I'd ask why are you using .Find instead of a more generic .Where or .Single? Why isn't GetParentCompany an extension method of IQueryable<Companie>? Commented Nov 24, 2015 at 18:09
  • @RobertMcKee, Findis not a linq in this case. Someone before me defined this method in the repository. I agree with you that it should have called differently. Commented Nov 24, 2015 at 19:26

2 Answers 2

3

Easiest way is to just remove these two lines:

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

You can also do this:

else if (clientIds.Length == 1)
{
     var clientId=clientIds[0];
     ccs = _xciRepository.Find<Companie>(x => x.CustCompID == clientId);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Most likely it's there is an easy way to solve your issue - just use a temporary variable, so your code will be like:

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.