For循环未返回期望值-C#-Blazor [英] For loop not returning expected value - C# - Blazor

查看:228
本文介绍了For循环未返回期望值-C#-Blazor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Blazor,我正在为我的项目创建一个分页功能.主要概念只是使用linq的.Skip()和.Take(),我将只获取列表中单击页面的列表项.

Using Blazor, I am creating a pagination feature for my project. The main concept is just using linq's .Skip() and .Take(), I will only take the list items for the page clicked from the list.

创建分页按钮的代码:(例如:1,2,3)

The code that creates the pagination buttons: (E.G: 1,2,3)

<ul class="pagination float-center">
  @for (int i = 0; i < Math.Ceiling((decimal)articleService.ReturnAll().Count() / numPerPage); i++)
    {
      <li class="page-item"><a class="page-link" onclick="@(() => ReturnPage(i))">@i</a></li>
    }
</ul>

我可以在这里使用0索引进行分页了.

I'm fine withing using 0 indexing here for my pagination.

上面的代码创建了一个按钮,按下这些按钮时(例如,第1页为第二页),传递for循环的索引,稍后我将其乘以每页的数量,以获取列表中元素的数量跳过.

This above code creates the buttons that when pressed (say page 1 which is the 2nd page), passes the index of the for loop which I will later multiply by the amount per page, to get the amount of elements in the list to skip.

例如,单击第1页时,我需要跳过列表中的2个元素,然后取下2个元素.(假设我希望每页2个元素作为量)

For example when clicking page 1, I will need to skip 2 elements in the list, then take the next 2. (Assuming I want 2 items as the amount per page)

然后,我的ReturnPage函数将跳过当前页面(单击的次数)-1,然后乘以2(或每页的数量)以找到要跳过的总金额.如果用户单击0(第一页),那么我需要.Skip(0),而只需.Take(每页金额).

Then my ReturnPage function will skip the current page (whats clicked) - 1, then multiply by 2 (or amount per page) to find the total amount to skip. If the user clicks 0 (first page) then I need to .Skip(0) and just .Take(amount per page).

void ReturnPage(int i)
 {

    articles = articleService.ReturnAll()
                                .OrderByDescending(x => x.LastUpdated)
                                    .Skip((i == 0) ? 0 : (i - 1) == 0 ? numPerPage : (i - 1) * numPerPage)
                                        .Take(numPerPage)
                                            .ToList();
 }

如果我有一个列表(文章),其中项目总数为3,则将其放在上下文中.这将使我的分页按钮变为0.1.第0页将显示2个项目,第1页将显示1个项目.

Putting this into context if I have a list (articles) with a total count of items at 3. This will return me pagination buttons of 0,1. page 0 will show 2 items, and page 1 will show 1 item.

如果单击分页按钮1,则应将1(i)传递给ReturnPage方法,这将计算2个项目的跳过量.表示此页面将跳过列表中的前2个项目,然后跳过下2个项目.(在这种情况下,只有1个项目)

If I click pagination button 1, I should pass 1 (i) to the ReturnPage method and that will calculate a skip amount of 2 items. Meaning this page will skip the first 2 items in the list, then take the next 2. (just 1 item in this case)

如果我单击返回到分页按钮0,则应传递i值0,不跳过任何内容,只取前2个项目.

If I click back to pagination button 0, I should pass an i value of 0, skipping nothing, and just take the first 2 items.

但是我根本看不到将正确的(i)值传递给ReturnPage函数.

However I am not seeing the correct (i) value being passed into the ReturnPage function at all.

运行我上面概述的相同示例,for循环返回正确数量的按钮:

Running the same example I outlined above, the correct amount of buttons are returned by the for loop:

但是在调试时,我的值为2(每次?).抛出了整个.Skip()功能.

But when debugging, i has a value of 2 (everytime?). Which throws off the whole .Skip() functionality.

我的解释是,完全不考虑linq的东西,for循环中的某些东西是错误的.这种情况对我来说似乎是正确的.如果我每页有3个和2个计数,即1.5,则意味着2页.所以我的i值应该为0和1,但不知何故为2?

My interpretation is, completely disregarding the linq stuff, something in the for loop is wrong. The condition seems correct to me. If I have a count of 3 and 2 per page, which is 1.5 which would mean 2 pages. So my i values should be 0 and 1, but somehow its 2?

我一定在这里缺少一些基本知识,有什么想法吗?

I must be missing something fundamental here, any ideas?

推荐答案

您的for循环应包含这样的局部变量:

Your for loop should contain a local variable like this:

 @for (int i = 0; i < Math.Ceiling((decimal)articleService.ReturnAll().Count() / numPerPage); i++)
    {
       var localVariable = i;

      <li class="page-item"><a class="page-link" onclick="@(() => ReturnPage(localVariable ))">@i</a></li>
    }

这是标准的C#行为,其中lambda表达式@(()=> ReturnPage(localVariable))可以访问变量,而不能访问变量的值.您必须定义一个for循环本地变量,否则您的lambda表达式将始终调用ReturnPage(i),并且在循环结束时我等于Math.Ceiling((decimal)articleService.ReturnAll().Count())

This is standard C# behavior where lambda expression @(() => ReturnPage(localVariable )) has access to a variable and not to the value of the variable. You have to define a variable which is local to the for loop, otherwise your lambda expression will always call ReturnPage(i) and i equals Math.Ceiling((decimal)articleService.ReturnAll().Count() at the end of the loop.

这篇关于For循环未返回期望值-C#-Blazor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆