【问题标题】:Why, the first query.ToList() work, but the second not work?为什么,第一个 query.ToList() 工作,但第二个不工作?
【发布时间】:2012-09-27 13:20:01
【问题描述】:

我的问题在于 ToLinq() 方法:

我不明白为什么第一个请求没有问题,但第二个给了我一个例外,例如:

(LINQ to Entities 不支持 LINQ 表达式 arrayIndex n 的节点类型)

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

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案1】:

    您不能将索引器与 LINQ to 实体一起使用。 您需要将该索引的值存储在一个新变量中。

    像这样:

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

    【讨论】:

      【解决方案2】:

      您的异常非常明确地说明了您的问题:您不能在 L2Entities 表达式中使用数组元素。

      【讨论】:

        【解决方案3】:

        您也可以通过这种方式使用 Equals 方法来实现它:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-05
          • 1970-01-01
          相关资源
          最近更新 更多