简体   繁体   中英

How do I access the data from an array of objects?

I am writing an API in C# with Visual Studios 2013 for Web.

So I have this model CurrentVisit:

public class CurrentVisit
{
    public Int64 visits_id { get; set; }
    public Int32 current_cycle_visits { get; set; }
    public Int32 total_visits { get; set; }
}

I use an SQL stored procedure to retrieve my data that I need:

object[] currentCycleArray = new object[visitIdArray.Length];
for (Int32 i = 0; i < currentCycleArray.Length; i++)
{
    var currentCycle = db.Database.SqlQuery<CurrentVisit>("uspApiGetCurrentVisit @member_id, @page_id, @visits_id", new SqlParameter("@member_id", memberAndPageIdentification.member_id), new SqlParameter("@page_id", memberAndPageIdentification.page_id), new SqlParameter("@visits_id", visitIdArray[i].visits_id));
    currentCycleArray[i] = currentCycle.ToArray();
}

Let's say visitIdArray.Length = 1;

object[] visits_balance = new object[visitIdArray.Length];
for(Int32 i = 0; i < visitIdArray.Length; i++)
{
   Int32 total_visits;
   for(Int32 j = 0; j < currentCycleArray.Length; j++)
   {
      if(currentCycleArray[j].visits_id == visitIdArray[i].visits_id)
      {
          total_visits = currentCycleArray[j].total_visits;
      }
   }
}

This seems to me like it's code that will work, but it does not. currentCycleArray[j].visits_id is not a valid property according to visual studio. The same goes for total_visits. I cannot acces my CurrentCycle fields. How is this happening?

UPDATED:

This does not work:

object[] currentCycleArray = new object[visitIdArray.Length];
for (Int32 i = 0; i < currentCycleArray.Length; i++)
{
    var currentCycle = db.Database.SqlQuery<CurrentVisit>("uspApiGetCurrentVisit @member_id, @page_id, @visits_id", new SqlParameter("@member_id", memberAndPageIdentification.member_id), new SqlParameter("@page_id", memberAndPageIdentification.page_id), new SqlParameter("@visits_id", visitIdArray[i].visits_id));
    currentCycleArray[i] = (CurrentVisit)currentCycle.ToArray();
}

OR this:

 object[] currentCycleArray = new object[visitIdArray.Length];
 for (Int32 i = 0; i < currentCycleArray.Length; i++)
 {
     var currentCycle = db.Database.SqlQuery<CurrentVisit>("uspApiGetCurrentVisit @member_id, @page_id, @visits_id", new SqlParameter("@member_id", memberAndPageIdentification.member_id), new SqlParameter("@page_id", memberAndPageIdentification.page_id), new SqlParameter("@visits_id", visitIdArray[i].visits_id));
     currentCycleArray[i] = (CurrentVisit)currentCycle;
 }

According to this DbRawSqlQuery Class , the DbRawSqlQuery implements IEnumerable< TElement>. This means you can iterate directly on the collection. So instead of having an object collection, you could make it a typed collection:

DbRawSqlQuery<CurrentVisit>[] currentCycleArray = new DbRawSqlQuery<CurrentVisit>[visitIdArray.Length];
for (Int32 i = 0; i < currentCycleArray.Length; i++)
{
    var currentCycle = db.Database.SqlQuery<CurrentVisit>("uspApiGetCurrentVisit @member_id, @page_id, @visits_id", new SqlParameter("@member_id", memberAndPageIdentification.member_id), new SqlParameter("@page_id", memberAndPageIdentification.page_id), new SqlParameter("@visits_id", visitIdArray[i].visits_id));
    currentCycleArray[i] = currentCycle;
}

Or you can keep the object collection, and cast the objects before accessing them:

object[] visits_balance = new object[visitIdArray.Length];
for(Int32 i = 0; i < visitIdArray.Length; i++)
{
   Int32 total_visits;
   for(Int32 j = 0; j < currentCycleArray.Length; j++)
   {
      var visit = (CurrentVisit)currentCycleArray[j]; // Cast to CurrentVisit
      if(visit.visits_id == visitIdArray[i].visits_id)
      {
          total_visits = visit.total_visits;
      }
   }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM