Iterate the list for the first half of the position

I try to iterate over the list only for the first half of the elements, and then again I want to iterate over the list only for the remaining half of the list. Any ideas?

@foreach (var category in Model.Categories.OrderBy(i => i.CategoryName))
{
  <li>
    <div id="category_@(category.SKU)" 
         class="itemBlock" 
         onclick="toggle('@(category.SKU)')">
  </li>
}
+3
source share
5 answers

Sort of:

var categories = Model.Categories.OrderBy(i => i.CategoryName).ToList();
int noOfCategories = categories.Count();
int half = noOfCategories/2;

for (int x = 0; x < half; x++)
{
    var category = categories[x];
    //your logic here
}
for (int x = half; x < noOfCategories; x++)
{
    var category = categories[x];
    //your logic here
}

Must do the trick, can not guarantee that the syntax is 100%, but this should give you everything you need to do!

+3
source

Just use for a loop .

for(int i=0; i < Model.Categories.OrderBy(i => i.CategoryName).Count/2; i++)
{
    // do stuff
}

for(int i=Model.Categories.OrderBy(i => i.CategoryName).Count/2; i < Model.Categories.OrderBy(i => i.CategoryName).Count; i++)
{
    // do different stuff
}
+2
source

, , :

[Serializable]
public class YourModel
{
    private int _mid
    {
        get { Categories.Count / 2; }
    }
    private int _top
    {
        get { Categories.Count - _mid; }
    }
    public List<Category> CategoriesLowerHalf
    {
        get { Categories.OrderBy(i => i.CategoryName).Take(_mid); }
    }
    public List<Category> CategoriesUpperHalf
    {
        get { Categories.OrderBy(i => i.CategoryName).GetRange(_mid, _top); }
    }
    // rest of your model
}

, :

@foreach (var category in Model.CategoriesLowerHalf)
{
   <li>
      <div id="category_@(category.SKU)" class="itemBlock" onclick="toggle('@(category.SKU)')">category.Name</div>
   </li>
 }

@foreach (var category in Model.CategoriesUpperHalf)
{
   <li>
      <div id="category_@(category.SKU)" class="itemBlock" onclick="toggle('@(category.SKU)')">category.Name</div>
   </li>
 }
+1

, 2 .

0

.

                      @{
                            int a = 0;
                            int b = 0;
                        }

                   <li>
                        @foreach (var item in Model.NameHere)
                        {
                           //write code here
                            a++;
                            if (a == Model.Offices.Count() / 2)
                            {
                                break;
                            }
                        }
                    </li>

                    <li>
                        @foreach (var item in Model.NameHere)
                        {

                            if (b >= Model.Offices.Count() / 2)
                            {
                                //write code here
                            }
                            b++;
                        }
                    </li>
0

All Articles