-1

I am trying to get all records as a List from For Loop. Inside that For Loop i have a query which returns single record everytime. Below is my code:

 IQueryable<MyClass> receive = null;
     for (int i = 0; i < selected_items.Count(); i++)
      {
        var idx = order_receive_id[selected_items[i]];
        receive = (from ors in db.item where ors.id == 
                   order_receive_idx 
                   select new MyClass()
                   {
                     quantity = ors.receive ?? 0,
                     total = ors.quantity * 250 ?? 0
                   });
      }

And then..

var com = new MyNewClass();
com.items_list = receive.ToList();

But this only shows the last record. I want all the records from above query outside the loop. I couldn't find any help according to my need.
Any help would be much appreciated.

2 Answers2

1

You Keep overwriting the variable receive.

recieve.AddRange((from ors in db.item where ors.id == 
                   order_receive_idx 
                   select new MyClass()
                   {
                     quantity = ors.receive ?? 0,
                     total = ors.quantity * 250 ?? 0
                   }));

Also if needed you can simplify the query with linq .

TBA
  • 1,077
  • 5
  • 41
  • 80
0

You may try Contains in Linq query like this

(from p in GetProducts()
       where prodIDs.Contains(p.ProductID)
       select p).ToArray<Product>();
Thirupathi cst
  • 127
  • 2
  • 10