Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架核心LINQ树的Concat表示问题_C#_.net Core_Entity Framework Core_Expression Trees - Fatal编程技术网

C# 实体框架核心LINQ树的Concat表示问题

C# 实体框架核心LINQ树的Concat表示问题,c#,.net-core,entity-framework-core,expression-trees,C#,.net Core,Entity Framework Core,Expression Trees,所以我有一个IQueryable扩展,它比这段代码做的更多。本质上,我是组合一堆字符串,然后对它们进行包含。我遇到的问题是Entity Framework Core不支持System.String.Concat,并在本地执行查询的那部分,这肯定不是我想要的 下面是我用来将这些不同字符串添加到一起的小循环: List<Expression> stringExpressionsToConcat = new List<Expression>();

所以我有一个IQueryable扩展,它比这段代码做的更多。本质上,我是组合一堆字符串,然后对它们进行包含。我遇到的问题是Entity Framework Core不支持System.String.Concat,并在本地执行查询的那部分,这肯定不是我想要的

下面是我用来将这些不同字符串添加到一起的小循环:

                List<Expression> stringExpressionsToConcat = new List<Expression>();
                foreach (var member in memberExpressions)
                {
                    stringExpressionsToConcat.Add(member);
                    stringExpressionsToConcat.Add(spaceConstant);
                }
                //call the concat to create the final term to search on
                NewArrayExpression arrayExpression = Expression.NewArrayInit(typeof(string), stringExpressionsToConcat);
                searchStringExpression = Expression.Call(concatMethod, arrayExpression);
这显然也没有转化为实体到SQL,因为这正是我在表达式树中构建的内容。然而,把它改成这个

.ThenByDescending(e => e.FirstName + " " + e.LastName)

将实体转换为SQL。因此,我想知道如何创建上面代码中表示的正确发送到SQL的相同表达式。我已尝试使用Expression.Add,但Expression builder中的字符串类型不支持Add。这是可能的,还是实体框架中有一些额外的代码使这成为可能?我曾尝试在GitHub上探索源代码,但要找到它的确切位置有点困难。

基本上:等待EfCore在某个时候支持它,或者现在就使用Ef classic

EfCore对LINQ的支持非常有限——感觉有点像开发它的人从来没有使用过它,也从来没有读过LINQ能读到的所有东西。没有解决办法

在EfCore 2.2中,它是在进行客户端评估,在3.1中,它被禁用,并告诉您去编写不同的程序


如果您喜欢,在github页面上打开一个问题,然后可能有人会在11月5.0版上找到它。机会是没有的,因为这是一个更神秘的问题,他们是超载的方式更常见。

啊哈!我已经弄明白了!!此表达式树显示相同的行为,并将数据发送到SQL

修改代码如下:

//more than one member has the property, we need to combine them with spaces in between
List<Expression> stringExpressionsToConcat = new List<Expression>();
foreach (var member in memberExpressions)
{
    stringExpressionsToConcat.Add(member);
    stringExpressionsToConcat.Add(spaceConstant);
}

searchStringExpression = stringExpressionsToConcat[0];
for (int i = 1; i < stringExpressionsToConcat.Count; i++)
{
    searchStringExpression = Expression.Add(searchStringExpression, 
    stringExpressionsToConcat[i], typeof(string).GetMethod("Concat", new[] { 
    typeof(string), typeof(string) }));
}
这正是Entity Framework生成SQL子句的方式,我在回答Order By时提到了这一点!我还不确定原因是什么……但如果您希望创建自己的表达式树,将实体框架核心树访问者可以转换为sql的字符串添加到一起,就是这样

这一回答为我指明了正确的方向,值得称赞:

那么,为了清楚起见,除了Concat之外,在表达式树中绝对没有办法表示stringType+stringType?我知道Concat不受支持,但在幕后发生了一些事情,允许stringType+stringType正确地转换为SQL,我只是不知道如何编写表达式树。之所以不知道,是因为它不是关于您编写的,而是关于您编写的,并且EfCore树访问者不会因为发现不受支持的节点集而引发异常。就这么简单。我弄明白了,然后贴出了答案。关于我的问题,我不确定我们是否意见一致。
//more than one member has the property, we need to combine them with spaces in between
List<Expression> stringExpressionsToConcat = new List<Expression>();
foreach (var member in memberExpressions)
{
    stringExpressionsToConcat.Add(member);
    stringExpressionsToConcat.Add(spaceConstant);
}

searchStringExpression = stringExpressionsToConcat[0];
for (int i = 1; i < stringExpressionsToConcat.Count; i++)
{
    searchStringExpression = Expression.Add(searchStringExpression, 
    stringExpressionsToConcat[i], typeof(string).GetMethod("Concat", new[] { 
    typeof(string), typeof(string) }));
}
([e].[MemberExpression1] + N' ') + [e].[MemberExpression2]) + N' ') + [e].[MemberExpression3]) + N' ')