2012-09-27 257 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])); 
相关问题