Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why, the first query.ToList() work, but the second not work?

my problem is with the ToLinq() method :

I don't understind why the first request work without problem, but the second give me an exception like :

(the node type of the LINQ expression arrayIndex n is not supported in LINQ to Entities)

            var q = from a in ctx.ImmImmobilisations select a;
            q = q.Where(x => x.CodeEntite      == "EDEF");
            q = q.Where(x => x.CodeAffectation == "000001");
            q = q.Where(x => x.Unite           == "ITS");
            q = q.OrderBy(x => x.CodeImmobilisation);
            List<ImmImmobilisation> res = q.ToList();

            var query = from e in ctx.ImmImmobilisations select e;
            if (!string.IsNullOrEmpty(args[0])) query = query.Where(x => x.CodeEntite      == args[0]);
            if (!string.IsNullOrEmpty(args[1])) query = query.Where(x => x.CodeAffectation == args[1]);
            if (!string.IsNullOrEmpty(args[2])) query = query.Where(x => x.CodeFamille     == args[2]);
            if (!string.IsNullOrEmpty(args[3])) query = query.Where(x => x.CodeCCout       == args[3]);
            if (!string.IsNullOrEmpty(unite))   query = query.Where(x => x.Unite      == unite);
            query = query.OrderBy(x => x.CodeImmobilisation);
            var ress = query.ToList();
like image 538
user1702980 Avatar asked Sep 27 '12 13:09

user1702980


2 Answers

you cant use indexers with LINQ to entities. You need to store the value of that index in a new variable.

like this:

var arg1 = args[0];    
if (!string.IsNullOrEmpty(arg1)) query = query.Where(x => x.CodeEntite == args1);
like image 115
Thousand Avatar answered Nov 16 '22 03:11

Thousand


Your exception states your problem quite explicitly: you can not use array elements inside L2Entities expression.

like image 32
Serg Rogovtsev Avatar answered Nov 16 '22 02:11

Serg Rogovtsev