gpt4 book ai didi

c# - 为什么,第一个 query.ToList() 工作,但第二个不工作?

转载 作者:太空狗 更新时间:2023-10-29 21:45:51 24 4
gpt4 key购买 nike

我的问题是 ToLinq() 方法:

我不明白为什么第一个请求没有问题,但第二个请求给我一个异常(exception):

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

最佳答案

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

像这样:

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

关于c# - 为什么,第一个 query.ToList() 工作,但第二个不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com