早上好
我在一个用于构建json的简单linq查询上扯我的头发:
JsonSerializer serializer = new JsonSerializer();
JObject myJSON = new JObject();
var result = new
{
test = from e in myTable
group e by new { e.col1, e.col2 } into g
select new
{
Code = g.Key.col1,
Name = g.Key.col2,
data = from e in myTable
where e.col1== g.Key.col1
select new
{
myField= e.field1
}
}
};
myJSON = JObject.FromObject(result, serializer);
当我尝试时,我得到了一个:
System.NotImplementedException: The method or operation is not implemented.
at NHibernate.Linq.GroupBy.NonAggregatingGroupByRewriter.FlattenSubQuery(SubQueryExpression subQueryExpression, QueryModel queryModel).
我的linq查询看起来没有那么复杂,但是我已经用尽了所有的尝试。
请把灯打开。
谢谢Sylvain
好了,终于找到了。只需要做以下操作:
JsonSerializer serializer = new JsonSerializer();
JObject myJSON = new JObject();
var result = new
{
test = from e in myTable.ToList()
group e by new { e.col1, e.col2 } into g
select new
{
Code = g.Key.col1,
Name = g.Key.col2,
data = from e in myTable
where e.col1== g.Key.col1
select new
{
myField= e.field1
}
}
};
myJSON = JObject.FromObject(result, serializer);
在第一个查询中使用. tolist()。
Sylvain