LINQ表达式中的聚合函数引发错误. (无法转换为商店表达式.) [英] Aggregate Function in LINQ expression throws error. (cannot be translated into a store expression.)

查看:79
本文介绍了LINQ表达式中的聚合函数引发错误. (无法转换为商店表达式.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误: LINQ to Entities无法识别方法'System.String Aggregate [String,String](System.Collections.Generic.IEnumerable 1[System.String], System.String, System.Func 3 [System.String,System.String,System.String])'方法和此方法无法转换为商店表达式.

Error: LINQ to Entities does not recognize the method 'System.String Aggregate[String,String](System.Collections.Generic.IEnumerable1[System.String], System.String, System.Func3[System.String,System.String,System.String])' method, and this method cannot be translated into a store expression.

Linq表达式:

      Items = context.TESTANSWER.Where(x => x.ID == 6729223232)
            .Join(context.QUESTIONREPOs, x => x.QUESTIONID, y => y.ID, (x, y) => new { x = x, y = y })
            .Join(context.OPTIONREPOs, p => p.x.QUESTIONID, q => q.QUESTIONID, (p, q) => new { p = p, q = q }).Where(p => p.p.x.RESPONSEID == p.q.ID)
            .GroupJoin(context.TESTANSWERASSOCIATION, c => c.p.x.ID, b => b.TESTANSWERID, (c, b) => new { c = c, b = b })
            .SelectMany(
                n => n.b.DefaultIfEmpty(),
                    (n, b) =>
                        new QuestListItemObj
                        {
                            State = n.c.p.x.STATE,
                            Association = n.b.Select(l => l.ASSOCIATION.TITLE).ToList().Aggregate((s, t) => s + ", " + t),
                            Description = n.c.p.y.DESCRIPTION,
                            Question = n.c.p.y.QUESTION,
                            Answer = n.c.q.OPTIONTEXT,
                        }).ToList();

我也尝试了SelectMany,但是出现了同样的错误.

I also just tried SelectMany but got same error..

 Affiliaiton = n.b.SelectMany(l => l.AFFILIATION.TITLE).Aggregate(string.Empty, (s, t) => s + ", " + t),

推荐答案

您有一个IQueryable可以转换为SQL.您的Aggregate是SQL未知的方法,因此无法翻译它,并且您会得到异常.

You're having an IQueryable that translates to SQL. Your Aggregate is a method that is unknown to SQL, so there is no way to translate it and you get your exception.

一种可能的方法是先调用AsEnumerable().这将导致查询执行并从SQL Server中获取数据,而其余操作将在内存中执行(而不是在SQL Server上).

A possible way is to call AsEnumerable() before. This will cause the query to execute and get the data from your SQL server, and the remaining actions are executed in memory (and not on your SQL Server).

myQuery.AsEnumerable().Aggregate(...)

这篇关于LINQ表达式中的聚合函数引发错误. (无法转换为商店表达式.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆