Using LINQ to loop through each index of an array and bind to a datasource
The following code works fine but it is only writing the last index[] to the datasource. I am having a hard time finding an elegant way to loop through each index and add/append it to the var mydata. It is a dynamic query, some may have zero indexes and some may have 5 or more...
My desired result would look something like this:
DetailsView1
ServerName: Server1, Server2, Server3
ServerIP: IP1, IP2, IP3
ServerState: State1, State2, State3
I am also up for other suggested methods, I am still a n00b at this coding stuff :)
Example:
if (result.Server.servicename !=null && result.Server.servicename.Length > 0)
{
foreach (string str in result.Server.servicename)
{
List<NetScalerResult> res = new NetScalerService(env).GetService(str);
var mydata = from r in res
select new
{
Name = r.Services.name,
ServerName = r.Services.servername,
ServerIP = r.Services.ipaddress,
ServerState = r.Services.svrstate,
Port = r.Services.port,
ServiceType = r.Services.servicetype,
ClientTimeout = r.Services.clttim开发者_StackOverflow中文版eout,
ServerTimeout = r.Services.svrtimeout
};
DetailsView1.DataSource = mydata;
DetailsView1.DataBind();
}
}
Thanks!! David
I don't quite understand the question. But it looks like you are data binding inside the foreach loop, which effectively overwrites the data binding from the previous iteration. So at the end, you are left with only the last entry being data-bound.
An easy was to fix this, is to collect the mydatas into a list and then data-bind that list.
精彩评论