0

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;
 }
tereško
  • 58,060
  • 25
  • 98
  • 150
user3861672
  • 55
  • 1
  • 1
  • 5
  • 1
    Your array is of type object, you need to make it of type CurrentVisit, or cast the content before accessing it. – sondergard Aug 21 '14 at 13:13
  • My array is of type object because the query returns a System.Data.Entity.Infrastructure.DbRawSqlQuery type. @sondergard – user3861672 Aug 21 '14 at 13:21
  • Either you need to make specified typed arrays, or cast the content - I will illustrate in an answer – sondergard Aug 21 '14 at 13:29
  • You need to create the CurrentVisit items manually, since SqlQuery doesn't return complex objects. Look at http://stackoverflow.com/questions/22359522/ef5-db-database-sqlquery-mapping-returned-objects for more details. – Patrick Aug 21 '14 at 13:31

1 Answers1

0

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;
      }
   }
}
sondergard
  • 3,184
  • 1
  • 16
  • 25