C#千万级数据实现条件筛选及分页功能
By
jerryxjr1220
at 2023-08-24 • 0人收藏 • 385人看过
C#的Linq实现大数据(千万级别)的筛选性能表现还是很不错的。
WPF界面:
<Window x:Class="WpfDemo01.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfDemo01" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <StackPanel> <ListBox x:Name="lb" Height="350" /> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,0"> <TextBox x:Name="pgNum" Text="Page" FontSize="20" Width="70"/> <Button Content="Go" Margin="10,0,0,0" FontSize="20" Width="50" Click="Button_Click" /> </StackPanel> </StackPanel> </Window>
逻辑部分:
namespace WpfDemo01 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public List<string> peopleList { get; set; } = new List<string>(); public int PageSize = 15; public MainWindow() { InitializeComponent(); for (int i = 0; i < 10000000; i++) { peopleList.Add($"Person {i+1}"); } lb.ItemsSource = peopleList.Take(PageSize); } private void Button_Click(object sender, RoutedEventArgs e) { var pg = Convert.ToInt32(pgNum.Text); lb.ItemsSource = peopleList.Where(x => Convert.ToInt32(x[7..]) > pg).Take(PageSize).ToList(); } } }
通过Where或者Skip可以非常方便的实现条件筛选或者跳转。
千万级数据量几乎没有什么明显延时。
登录后方可回帖