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

查看:31
本文介绍了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.

创建分页按钮的代码:(E.G: 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) 并且在循环结束时 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天全站免登陆