我有两个foreach运营商:
1st - 查找所有可见行并将数据插入其中;
第二个 - 来自CAML查询的foreach行并获取数据。
我需要foreach 来自查询的第一行并插入第一个可见行中的数据,来自查询的第二行并在第二个可见行 .... 这是我的代码是:
[ResponseType(typeof(UserModel))]
public IHttpActionResult Get(string id) {
var user = db.AspNetUsers.FirstOrDefault(s => s.UserName == id);
if (user == null)
return NotFound();
var data = new UserModel {
UserName = user.UserName,
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName,
Roles = user.AspNetRoles.Select(s => s.Name).ToArray()
//...other properties you want to return
};
return Ok(data);
}
public class UserModel {
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string[] Roles { get; set; }
}
代码的结果是:
如何解决这个问题,谢谢你以前......
答案 0 :(得分:1)
循环看起来正确,逻辑错误。您只需将所有记录放入所有列。最后你只能看到最后的记录。
int counter = 0;
foreach (TableRow tr in allVisibleRows)
{
destination = myTableCol[counter]["Route"].ToString();
dateGo = myTableCol[counter]["DepartureDate"].ToString();
dateEnd = myTableCol[counter]["ReturnDate"].ToString();
kmInCity = myTableCol[counter]["TraveledInCity"].ToString();
kmOutCity = myTableCol[counter]["TraveledOutCity"].ToString();
((TextBox)tr.Cells[1].Controls[0]).Text = destination;
((DateTimeControl)tr.Cells[2].Controls[0]).SelectedDate = DateTime.Parse(dateGo);
((DateTimeControl)tr.Cells[3].Controls[0]).SelectedDate = DateTime.Parse(dateEnd);
((TextBox)tr.Cells[4].Controls[0]).Text = kmInCity;
((TextBox)tr.Cells[5].Controls[0]).Text = kmOutCity;
counter++;
}