2012-09-27 9 views
6

mein Problem ist, mit dem ToLinq() -Methode:Warum funktioniert die erste query.ToList(), aber die zweite nicht?

ich understind nicht, warum ohne Problem die erste Anforderung Arbeit, aber die zweite mir eine Ausnahme geben wie:

(Knotentyp des LINQ Ausdruck Arrayindex n in LINQ to Entities nicht unterstützt)

  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(); 

Antwort

1

Ihre Ausnahme besagt, Ihr Problem ganz explizit: Sie können nicht Array-Elemente innerhalb L2Entities Ausdruck verwenden.

3

Sie können keine Indexer mit LINQ zu Entitäten verwenden. Sie müssen den Wert dieses Index in einer neuen Variablen speichern.

wie folgt aus:

var arg1 = args[0];  
if (!string.IsNullOrEmpty(arg1)) query = query.Where(x => x.CodeEntite == args1); 
0

Sie können es auch erreichen, indem die Equals-Methode auf diese Weise mit:

if (!string.IsNullOrEmpty(args[0])) 
    query = query.Where(x => Equals(x.CodeEntite, args[0])); 
Verwandte Themen