如何对 SQLite 数据库中的列表求和

How do I Sum a List from SQLite database

我正在尝试将分数的总和从数据库打印到 Label,但我无法打印出来 运行。

我使用 Entry 作为分数和姓名的输入,然后使用按钮功能将它们存储到数据库的表中。它看起来像这样:

public void Next_round(object sender, System.EventArgs e)
        {
            Score score = new Score()
            {
                score1 = scoreEntry1.Text,
                score2 = scoreEntry2.Text
            };

            using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
            {
                conn.CreateTable<Score>();
                conn.Insert(score);
            }
        } 

using System;
using SQLite;

namespace App1.Classes
{
    [Table("Player")]
    public class Player
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        public string pName1 { get; set; }

        public string pName2 { get; set; }

    }
    [Table("Score")]
    public class Score
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string score1 { get; set; }
        public string score2 { get; set; }
    }
}

XAML 页

<ListView x:Name="playersListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Label Grid.Column="0" Grid.Row="0" Text="{Binding pName1}" /FontAttributes="Bold" /> //Here is the players name
                            <Label Grid.Column="0" Grid.Row="0" Text="{Binding pScore1}" /> //And here's his score
                            

                            <Label Grid.Column="1" Grid.Row="0" Text="{Binding pName2}" FontAttributes="Bold" />
                            <Label Grid.Column="1" Grid.Row="0" Text="{Binding pScore2}"/>
                            
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label x:Name="fScore1" /> //here should be the sum

我曾尝试使用 .ToList() 将数据库放入一个列表中,然后将其放入另一个列表中以使 .Sum() 方法起作用,但它没有起作用

.cs文件

namespace App1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page4 : ContentPage
    {
        public Page4()
        {
            InitializeComponent();
            
        }
        private void Reset(object sender, EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new MainPage());
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();

            using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
            {
                conn.CreateTable<Player>();
                var players = conn.Table<Player>().ToList();
                conn.CreateTable<Score>();
                List<Score> scores = conn.Table<Score>().ToList();

                List<int> list = new List<int>();
                list.AddRange((IEnumerable<int>)scores);//here i tried adding it into another table 
but it throws an exception "System.InvalidCastException: 'Specified cast is not valid.'"

                string res = list.Sum().ToString();

                fScore1.Text = res;
                playersListView.ItemsSource = players;
            }
        }
    }
}

如果有人能帮助我,我将不胜感激

List<int> list = new List<int>();
list.AddRange((IEnumerable<int>)scores);

由于scores的类型是List<Score> scores ,所以不能直接将scores转换成List<int>类型。

如果要计算列表scores中每个score1的总和,可以遍历scores,得到每一项score1

请参考以下代码:

        List<Score> scores = conn.Table<Score>().ToList();
        
        int score1_sum = 0 ;
        foreach (Score score in scores) {
           score1_sum +=  Int16.Parse(score.score1);
          }

        fScore1.Text = score1_sum.ToString();