0

我有一个返回用户列表的类

public IList<RegisterUsers> GetUsers()
{
    var query = (from up in _context.UserProfile
                     join um in _context.Membership on up.UserID equals um.UserID
                     select new RegisterUsers { ID = ua.UserID, FirstName = ua.FirstName, LastName = ua.LastName, LastLogIn = up.LastLogIn, Status = up.Status });

    return query.ToList();
}

我通过在我的 Action 上执行以下操作来调用这个类

var UserRepository = new UserRepository();
var AllUsers = UserRepository.GetUsers();


var result = from c in AllUsers
                select new List<string> { c.ID.ToString(), c.FirstName, c.LastName, c.LastLogIn,c.Status };


return Json(new { sEcho = param.sEcho,
                  iTotalRecords = result.Count(),
                  iTotalDisplayRecords = result.Count(),
                  aaData = result
                },
JsonRequestBehavior.AllowGet);

上面的代码工作正常,它返回 JSON 数据。但是我试图弄清楚如果我只是将 AllUsers 直接附加到 aaData 变量,为什么会出现错误。请看下文。

var UserRepository = new UserRepository();
var AllUsers = UserRepository.GetUsers();

return Json(new { sEcho = param.sEcho,
                  iTotalRecords = AllUsers.Count(),
                  iTotalDisplayRecords = AllUsers.Count(),
                  aaData = AllUsers
                },
JsonRequestBehavior.AllowGet);
4

1 回答 1

0

我能够通过执行以下操作返回列表

var AllUsers = UserRepository.GetUsers().Select(p => new List<string> { p.UserID.ToString(), p.FirstName, p.LastName }).ToList();

基本上上面的代码只是消除了下面的代码行

var result = from c in AllUsers
            select new List<string> { c.ID.ToString(), c.FirstName, c.LastName, c.LastLogIn,c.Status };

希望我能够回答你的问题。

于 2013-11-08T18:27:05.287 回答