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_Jqgrid - Fatal编程技术网

C# 从Linq返回的子数组不正确

C# 从Linq返回的子数组不正确,c#,linq,jqgrid,C#,Linq,Jqgrid,为jqGrid准备数据我得到了子网格的意外单元格数组。简化的代码如下所示: var result = new { total = 1, page = 1, records = qstList.Count(), rows = qstList.Select(( c, i ) => new { Id = c.QuestionId, Text = c.Text,

为jqGrid准备数据我得到了子网格的意外单元格数组。简化的代码如下所示:

var result = new
{
    total = 1,
    page = 1,
    records = qstList.Count(),
    rows =  qstList.Select(( c, i ) => new
            {
                Id = c.QuestionId,
                Text = c.Text,
                Type = c.Type,
                Points = c.Points,
                Ordinal = c.Ordinal,
                subgrid = new
                {
                    subtotal = 1,
                    subpage = 1,
                    cell = qstList.Where(
                              q => q.QuestionId == c.QuestionId).Select(
                              q => q.Answers).Select((d, j) => new
                              {
                                  Id = d.Select(a => a.AnswerId),
                                  Text = d.Select(a => a.Text),
                                  Correctness = d.Select(a => a.Correctness),
                                  Ordinal = d.Select(a => a.Ordinal)
                              }).ToArray()
                }
            }).ToArray()
};
行很好,但子网格的单元格数组是奇数。我期望的是:

{[Id, Text, Correctness, Ordinal], ..., [Id, Text, Correctness, Ordinal]}
但事实证明:

{[Id, Id, ...], ..., [Ordinal, Ordinal, ...]}

如何获得预期的“布局”。谢谢你的帮助

这将引导您找到正确的答案:

  • 为所有匿名类(行、子网格、Cel等)创建普通类
  • 重构所有代码,使一行中只有一条Linq语句

现在调试als就容易多了,您可以看到所有Linq语句的每个子结果。我相信你会通过这种方式找到正确的答案。(如果没有,只需将不起作用的Linq语句添加到您的帖子中即可)。

@Vladimir,谢谢!是的,SelectMany可以:

subgrid = new
{
    subtotal = 1,
    subpage = 1,
    cell = qstList.Where(q => q.QuestionId == c.QuestionId).SelectMany(q => q.Answers).Select((d, j) => 
    new 
    {
        Id = d.AnswerId,
        Text = d.Text,
        Correctness = d.Correctness,
        Ordinal = d.Ordinal
     }).ToArray()
  }

我可以通过添加.First()获得预期的“布局”。所以Id=d.Select(a=>a.AnswerId).First()。还有一个问题,为什么我需要它来表示单元格而不是行。对于单元格,您有IEnumerable-double序列。请尝试
…q=>q.Answers)。选择many((d,j)=>d.Select(a=>new{Id=a.AnswerId,Text=a.Text,…})。ToArray()