con001.Open();
SqlCommand cmd001 = new SqlCommand("select distinct TruckRegistration.Id from booking_txn inner join Payments on booking_txn.booking_ID=Payments.booking_ID inner join truck_log on booking_txn.booking_ID=truck_log.booking_id inner join TruckRegistration on TruckRegistration.Id=truck_log.truck_id inner join booking_master on booking_master.booking_ID=booking_txn.booking_ID where booking_master.booking_pickupdate between '10-Jul-2016' and '10-Aug-2016' ", con001);
DataTable myDataTable = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter(cmd001);
con001.Close();
ad.Fill(myDataTable);
List<String> trucklist = new List<String>();
foreach (DataRow dataRow in myDataTable.Rows)
{
trucklist.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));
}
*I Have stored values from a datatable to trucklist array*
After this what is to be done is, to store every value in array to seperate string using for loop.
foreach (String currentTruck in truckList)
{
// do something with currentTruck
}
or
for (int i = 0; i < truckList.Count; i++)
{
String currentTruck = truckList[i];
// do something with currentTruck
}
Your example is similar to the code below, which will just fill 2,3,4,5
at different indexes of the stringList
. If beside Id
one more column is added to the class A
, then would have some impact of string.Join
void Main()
{
List<A> intList = new List<A> { new A { Id = 2}, new A { Id = 3}, new A { Id = 4}, new A { Id = 5}};
List<string> stringList = new List<string>();
var propertyArray = intList.First().GetType().GetProperties();
foreach (var x in intList)
{
stringList.Add(string.Join(";",propertyArray.Select(y => y.GetValue(x,null))));
}
// Print StringList
}
public class A
{
public int Id { get; set;}
}
Edit
Convert to Array
, use following code:
var stringArray = stringList.ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.