0

我正在尝试从返回数据集的 Web 服务创建 JSON 响应。如何遍历数据表并将这些值放入客户端模型中?当我尝试遍历数据表时,我收到无效的启动器错误。这是我的代码:

public class ClientsController : ApiController
{
    public IEnumerable<ClientResponseModels> GetAllClients()
    {  
        //retrieves list of all the clients
        var dsClients = ClientRepository.GetAllClients();

        //create a json response object
        ClientResponseModels[] response = new ClientResponseModels[]
        {
            new ClientResponseModels
            {
                UserId= userDetails.UserID,
                UserName=userDetails.LoginName,
                LocationName=centreLocation.ToString(),
                Clients = new List<ClientModels>
                {  
                    foreach(DataRow row in dsClients.Tables[0].rows)
                    {
                        new ClientModels //these values need to come from datatable
                        {
                            Id =Convert.ToInt32(row["client_id"]), 
                            ClientName=Convert.ToString(row["client_nme"])", 
                            DOB= Convert.ToDateTime(row["birth_date]")
                        }
                    }    
                } 
            }
        };
        return response;
    }
    else {
        throw new HttpResponseException(HttpStatusCode.Forbidden);
    }
}
4

2 回答 2

0

我认为该函数是您的代码循环遍历可查询的原因。您可以在循环之前先检索数据,例如 GetAllClients().ToList()

于 2014-11-29T23:30:26.917 回答
0

我使用实体框架更改数据上下文配置。

        MyDataContext db = new MyDataContext();
        db.Configuration.LazyLoadingEnabled = false;
        db.Configuration.ProxyCreationEnabled = false;
于 2013-05-03T05:45:57.923 回答