如何从对象数组中访问数据?

时间:2014-08-21 13:10:01

标签: c# arrays asp.net-mvc visual-studio

我正在使用Visual Studios 2013 for Web在C#中编写API。

所以我有这个模型CurrentVisit:

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

我使用SQL存储过程来检索我需要的数据:

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();
}

让我们说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;
      }
   }
}

在我看来,它的代码可以正常使用,但事实并非如此。根据visual studio,currentCycleArray [j] .visits_id不是有效的属性。 total_visits也是如此。我无法访问我的CurrentCycle字段。这是怎么回事?

更新:

这不起作用:

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();
}

或者这个:

 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;
 }

1 个答案:

答案 0 :(得分:0)

根据这个DbRawSqlQuery Class,DbRawSqlQuery实现IEnumerable&lt; TElement取代。这意味着您可以直接迭代集合。因此,您可以将其设为类型集合,而不是拥有对象集合:

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;
}

或者您可以保留对象集合,并在访问对象之前强制转换对象:

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;
      }
   }
}