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;
 }
4
  • 1
    Your array is of type object, you need to make it of type CurrentVisit, or cast the content before accessing it. Commented Aug 21, 2014 at 13:13
  • My array is of type object because the query returns a System.Data.Entity.Infrastructure.DbRawSqlQuery<CurrentVisit> type. @sondergard Commented Aug 21, 2014 at 13:21
  • Either you need to make specified typed arrays, or cast the content - I will illustrate in an answer Commented Aug 21, 2014 at 13:29
  • You need to create the CurrentVisit items manually, since SqlQuery doesn't return complex objects. Look at stackoverflow.com/questions/22359522/… for more details. Commented Aug 21, 2014 at 13:31

1 Answer 1

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;
      }
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You are the man! I kept my object array and casted the objects before accessing them. Works like a charm. You saved me some migraines @sondergard
I had to cast it as var visit = (CurrentVisit[])currentCycleArray[j]; and then loop through them because it was an array of arrays. @sondergard
Ok sure, but you got the hang of it now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.