Hi, Im having a problem on accessing the list in the class. Im still new to c#. So I need help to solve this issue.
Below is the classes i've created. In class Datum, there's a list called location.
public class Location
{
public string location { get; set; }
public string remarks { get; set; }
public string deliverydate { get; set; }
public string pickupdate { get; set; }
}
public class Datum
{
public string tracking_number { get; set; }
public string pickup_place { get; set; }
public string delivery_place { get; set; }
public string tel_no { get; set; }
public string status { get; set; }
public DateTime lastupdate_time { get; set; }
public List Location { get; set; }
}
public class Root
{
public List data { get; set; }
}
I want to use the List
'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?). at line
foreach ( var p in data.Root)
{
string tracking_number = p.tracking_number;
string pickup_place = p.pickup_place;
string delivery_place = p.delivery_place;
string tel_no = p.tel_no ;
string status = p.status ;
datetime lastupdate_time = p.lastupdate_time ;
// start from this line, gives CS1061 error
string location = p.Location.location ;
string remarks = p.Location.remarks ;
string deliverydate = p.Location.deliverydate;
string pickupdate = p.Location.pickupdate;
query = "INSERT INTO Tracking (tracking_number, pickup_place, delivery_place, tel_no, status,
lastupdate_time, location, remarks, deliverydate , pickupdate) VALUES (tracking_number,
pickup_place, delivery_place, tel_no, status, lastupdate_time, location, remarks, deliverydate ,
pickupdate);
}
How can I solve this problem? I've tried to look for solutions, but nothing works.

Nitin SontakkeRank #264 · 9.6K reputationPosted Feb 2, 2022, 10:40 PM EST
Hello,
If there is one to many relation between Datum and Location, how exactly are you visualizing to make it flat and store it in single table?
Anyway.
Because there is a list of Location in Datum, you will need to access it, just as an example, as follows:
string location = p.Location[0].location;
string remarks = p.Location[0].remarks;
string deliverydate = p.Location[0].deliverydate;
string pickupdate = p.Location[0].pickupdate;
But remember that this only picks first Location from many in EVERY iteration of p.
You will really need a strategy of storing this info in related tables.
As Ak74 reputationPosted Feb 2, 2022, 11:07 PM EST
@Nitin Sontakke, thank you for your reply.
But, for storing the values of location into the database, if there's 10 different location, does it mean I need to do another foreach before string location = p.Location.location; ?
Nitin SontakkeRank #264 · 9.6K reputationPosted Feb 2, 2022, 11:55 PM EST
As Ak74 reputationPosted Feb 3, 2022, 1:08 AM EST
@Nitin, I've tried to put foreach before that line, but now im having an issue, the data is being duplicated.
Nitin SontakkeRank #264 · 9.6K reputationPosted Feb 3, 2022, 2:11 AM EST
As Ak74 reputationPosted Feb 3, 2022, 2:30 AM EST
@Nitin, thank you for your help. Its already working now.