manveruPL
manveruPL

Reputation: 57

Error while executing statement in ASP.NET MVC4 LINQ

Hello I have an error when I'am trying to select particular data from db Table in MVC 4 Application. Error code: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

Code causing that error:

protected Array getOddzialy(float x, float y, string[] TypyOddzialow)
    {
        Array ret = null;

        var oddzialy = from s in dbOddzial.Oddzialy select s;

        float maxX = x + (float)0.01;
        float maxY = y + (float)0.01;
        float minX = x - (float)0.01;
        float minY = y - (float)0.01;

        oddzialy = oddzialy.Where(s => s.LocX.CompareTo(maxX) < 0 && s.LocX.CompareTo(minX) > 0 && s.LocY.CompareTo(maxY) < 0 && s.LocY.CompareTo(minY) > 0);

        if (TypyOddzialow.Count() == 1)
            oddzialy = oddzialy.Where(s => s.Type.Contains(TypyOddzialow[0]));
        else if (TypyOddzialow.Count() == 2)
            oddzialy = oddzialy.Where(s => s.Type.Contains(TypyOddzialow[0]) || s.Type.Contains(TypyOddzialow[1]));
        else
            oddzialy = oddzialy.Where(s => s.Type.Contains(TypyOddzialow[0]) || s.Type.Contains(TypyOddzialow[1]) || s.Type.Contains(TypyOddzialow[2]));
        ret = oddzialy.ToArray();

        return ret;
    }

I search for answer but everything I found don't work. Please help.

Upvotes: 0

Views: 79

Answers (1)

mreyeros
mreyeros

Reputation: 4379

You will need to store the values of:

 var typyOddzialowFirst = TypyOddzialow[0]
 var typyOddzialowSecond = TypyOddzialow[1]
 var typyOddzialowThird = TypyOddzialow[2]

In separate local variables and use them that way in your linq queries.

 if (TypyOddzialow.Count() == 1)
        oddzialy = oddzialy.Where(s => s.Type.Contains(typyOddzialowFirst));
    else if (TypyOddzialow.Count() == 2)
        oddzialy = oddzialy.Where(s => s.Type.Contains(typyOddzialowFirst) || s.Type.Contains(typyOddzialowSecond));
    else
        oddzialy = oddzialy.Where(s => s.Type.Contains(typyOddzialowFirst) || s.Type.Contains(typyOddzialowSecond) || s.Type.Contains(typyOddzialowThird));
    ret = oddzialy.ToArray();

Upvotes: 2

Related Questions