title: How can I define a List to add results of a query in a loop?
body: <p>I have an array filled with long type values and for each value in the array I need to implement a query. I used foreach loop as you can see from the code below:</p>
  
  <pre><code>var result;
          foreach(long id in PrdIdArr)
          {
              var mainQuery = (from o in db.OPERATIONs
                               join u in db.UNITs on o.OP_UNIT_ID equals u.UNIT_ID into smt
                               from s in smt
                               join x in db.XIDs on s.UNIT_ID equals x.UNIT_ID
                               where o.OP_OT_CODE == OtCode
                               where x.IDTYP_CD == "BSN"
                               where s.START_PRD_ID == id
                               where o.OP_UPD_DATE &gt;= _StartDate
                               where o.OP_UPD_DATE &lt;= _EndDate
                               select new
                               {
                                   o.OP_ID,
                                   o.OP_UPD_DATE,
                                   x.EXTERNAL_ID,
                                   o.OP_OS_CODE,
                                   o.OP_START,
                                   o.OP_ST_STATION,
                                   s.START_PRD_ID
                               }).Take(_RowNumber);
              //var result = mainQuery.ToList();
              result.add(mainQuery.ToList());
          }
  
          data = this.Json(result);
          data.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
          return data;
  </code></pre>
  
  <p>However, I have a problem in my code; I have to define a main list just before the foreach loop so that I could add results of each query to the that main list. my question is: How can I define this list as you can see at the beginning of my code? Thanks for the help...</p>