2012-09-27 77 views
6

我的問題是與ToLinq()方法:爲什麼第一個query.ToList()工作,但第二個不工作?

我不understind爲什麼沒有問題的第一個請求的工作,但第二個給我像一個例外:

(LINQ的表達式的節點類型arrayIndex n爲不支持LINQ到實體)

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

回答

1

你的例外相當明確指出你的問題:你不能使用L2Entities表達式中的數組元素。

3

你不能使用索引器與LINQ到實體。 您需要將該索引的值存儲在新變量中。

這樣的:

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

您也可以通過使用Equals方法這種方式實現它:

if (!string.IsNullOrEmpty(args[0])) 
    query = query.Where(x => Equals(x.CodeEntite, args[0])); 
相關問題