Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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# 如何让我的jsonResult返回一个复杂的json返回?_C#_Json_Linq - Fatal编程技术网

C# 如何让我的jsonResult返回一个复杂的json返回?

C# 如何让我的jsonResult返回一个复杂的json返回?,c#,json,linq,C#,Json,Linq,我有一个linq查询,可以返回数据,它工作得很好,但是我无法将数据返回到浏览器。它抛出了一个极其可怕的错误: LINQ to Entities无法识别方法“System.Collections.Generic.List1[System.Int32]ToList[Int32](System.Collections.Generic.IEnumerable1[System.Int32]),此方法无法转换为存储表达式 我以前多次遇到过这个错误,但我还没有找到解决方法,更重要的是,我还没有找到它发生的原因

我有一个linq查询,可以返回数据,它工作得很好,但是我无法将数据返回到浏览器。它抛出了一个极其可怕的错误:

LINQ to Entities无法识别方法“System.Collections.Generic.List
1[System.Int32]ToList[Int32](System.Collections.Generic.IEnumerable
1[System.Int32]),此方法无法转换为存储表达式

我以前多次遇到过这个错误,但我还没有找到解决方法,更重要的是,我还没有找到它发生的原因

这是我的疑问:

var query = (from c in db.Responses
   join d in db.ResponseDetails on c.ResponseId equals d.ResponseId
   join f in db.UserProfiles on c.UserProfile.UserId equals f.UserId
           where c.QuestionId == id && f.UserId.Equals(userid)
   select new TeamForecastHistory { Id = d.ResponseId,
                               Reason = c.Reason,
                               UserName = f.UserName,
                               DateCreated = c.DateCreated,
                               Values = (from l in db.ResponseDetails 
                                      where l.ResponseId == id
                                      select (int) l.ResponseDetailVal*100  ).ToList()
                            }).ToList();
这是我想要发送回数据的模型:

public class TeamForecastHistory
        {
            public int Id { get; set; }
            public int Userid { get; set; }
            public DateTime DateCreated { get; set; }
            public string Reason { get; set; }
            public string UserName { get; set; }
            public List<int> Values { get; set; }
        }
更新

按照john的建议,我稍微更改了模型,现在它不是
List
,而是
IEnumerable Values
。这会返回数据,不会给出错误,但也不会返回值的数据:(这是返回数据,代表数据集)

更新

上次更新提到values变量没有返回任何数据,这是因为where子句中的子查询使用了错误的id。下面列出了工作的完整代码:

var query = (from c in db.Responses
         join d in db.ResponseDetails on c.ResponseId equals d.ResponseId
         join f in db.UserProfiles on c.UserProfile.UserId equals f.UserId
         where c.QuestionId == id && f.UserId.Equals(userid)
         select new TeamForecastHistory { Id = d.ResponseId,
                                     Reason = c.Reason,
                                     UserName = f.UserName,
                                     DateCreated = c.DateCreated,
                                     Values = (from l in db.ResponseDetails 
                                              where l.ResponseId == d.ResponseId
                                              select l.ResponseDetailVal*100  )
           }).ToList();

它试图在数据库上执行内部ToList。SQL server不知道如何做到这一点。您可以尝试只获取数据,然后在获取结果后构建输出模型。

只需关闭ToList。@Johns了解如果我关闭ToList,它会说:无法隐式地将类型“System.Linq.IQueryable”转换为“System.Array”。存在显式转换(是否缺少强制转换?)。尝试将
Values
属性设置为类型
IEnumerable
,那么您就不需要
Values=
上的
.ToList()
。这只意味着子查询返回norows@JohnSaunders是的,在子查询的where子句中犯了一个愚蠢的错误。我想用d.responseid代替id。
0:{
    DateCreated: "/Date(1389222371636)/"
    Id: 122
    Reason: "Reason#1"
    UserName: "jsteve"
    Userid: 0
    Values: []
}
var query = (from c in db.Responses
         join d in db.ResponseDetails on c.ResponseId equals d.ResponseId
         join f in db.UserProfiles on c.UserProfile.UserId equals f.UserId
         where c.QuestionId == id && f.UserId.Equals(userid)
         select new TeamForecastHistory { Id = d.ResponseId,
                                     Reason = c.Reason,
                                     UserName = f.UserName,
                                     DateCreated = c.DateCreated,
                                     Values = (from l in db.ResponseDetails 
                                              where l.ResponseId == d.ResponseId
                                              select l.ResponseDetailVal*100  )
           }).ToList();