Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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表达式中的聚合函数引发错误。(无法转换为存储表达式。)_C#_Linq_Entity Framework_Linq To Sql_Aggregate Functions - Fatal编程技术网

C# LINQ表达式中的聚合函数引发错误。(无法转换为存储表达式。)

C# LINQ表达式中的聚合函数引发错误。(无法转换为存储表达式。),c#,linq,entity-framework,linq-to-sql,aggregate-functions,C#,Linq,Entity Framework,Linq To Sql,Aggregate Functions,错误: LINQ to Entities无法识别方法“System.String Aggregate[String,String](System.Collections.Generic.IEnumerable1[System.String],System.String,System.Func3[System.String,System.String,System.String]),此方法无法转换为存储表达式 Linq表达式: Items = context.TESTANSWER.Wh

错误: 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]),此方法无法转换为存储表达式

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,但得到了相同的错误

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

您有一个可转换为SQL的
IQueryable
。您的
Aggregate
是一个SQL未知的方法,因此无法转换它,您会得到异常

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


正如错误消息告诉您的,数据库不知道如何将代码转换为SQL

幸运的是,它真的没有必要这么做。不要将数据放在DB端的逗号分隔字符串中,只需将这些片段拉下来,用C#将其生成一个字符串即可。它提取的数据量相同,因此没有真正的理由使用数据库

您可以使用
AsEnumerable
来确保以下操作是linq to object中的操作,而不是在DB端,但在这种情况下,
Aggreagte
是一个不好的工具,无法用于将值追加到字符串。只需使用
String.Join

var query = n.b.SelectMany(l => l.AFFILIATION.TITLE);


//not very efficient option, but will work
string data1 = query.AsEnumerable().
    .Aggregate(string.Empty, (s, t) => s + ", " + t);

//faster, more efficient, simpler to write, and clearer to the reader.
string data2 = string.Join(", ", query);
var query = n.b.SelectMany(l => l.AFFILIATION.TITLE);


//not very efficient option, but will work
string data1 = query.AsEnumerable().
    .Aggregate(string.Empty, (s, t) => s + ", " + t);

//faster, more efficient, simpler to write, and clearer to the reader.
string data2 = string.Join(", ", query);