将集合值和数据库中的返回值进行比较

本文关键字:返回值 比较 集合 数据库 | 更新日期: 2023-09-27 18:19:39

我有以下类:

    public class players
    {
        public void LottoDraw(object sender, EventArgs e)
        {
            var connectionstring = "Server=C;Database=lotto;User Id=lottoadmin;Password=password;";

            using (var con = new SqlConnection(connectionstring))  // Create connection with automatic disposal
            {
                con.Open();
                // Create new DataTable
                DataTable player = new DataTable();
                {
                    // Create new DataAdapter
                    using (SqlDataAdapter a = new SqlDataAdapter("SELECT TOP 1 LOTTOID, VAL0, VAL1, VAL2, VAL3, VAL4, VAL5 FROM tblLotto ORDER BY NEWID()", con))
                    {
                        // Use DataAdapter to fill DataTable
                        a.Fill(player);
                    }
                }
            }
        }
        public int val0, val1, val2, val3, val4, val5, val6;
        public IEnumerable<int> Numbers
        {
            get
            {
                return new[] { val0, val1, val2, val3, val4, val5, val6 };
            }
        }
    }
}

它将从数据库中返回一个带有一些乐透号码的随机行。然后,这些值将被放入一个集合中。

然后我想用一些林克来比较获胜的数字和所画的数字。

public void LottoWinners(object sender, EventArgs e)
        {
            Dictionary<int, int> number = new Dictionary<int, int>();
            Random generator = new Random();
            while (number.Count < 1)
            {
                number[generator.Next(1, 49)] = 1;
            }
            string[] lotto = number.Keys.OrderBy(n => n).Select(s => s.ToString()).ToArray();
            var winners = players.
                Where(Lotto => Lotto.Players.Numbers.Intersect(lotto).Count() >= 3);
            //write some logic to find out who has 3 match numbers 
            //and assign there ticket to the winners table tblWinners
        }

然而,我得到了一个错误:

"底部玩家"不包含"Where"的定义

我的所有文件中都已包含:using System.Linq;

将集合值和数据库中的返回值进行比较

看起来您正试图对players类使用Where扩展方法。Where针对实现IEnumerable的集合进行操作。

Lotto.players是一个类,它正确地通知您在Lotto.players类上没有名为Where的扩展方法。IEnumerable<int> Numbers属性将允许使用Where