0

我有以下课程:

    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 };
            }
        }

    }
}

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

然后,我想使用一些 Linq 将中奖号码与抽出的号码进行比较。

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

        }

但是我得到一个错误:

“Lotto.players”不包含“Where”的定义

我已经有:using System.Linq;在我所有的文件中!

4

2 回答 2

1

看起来您正在尝试对类使用Where扩展方法playersWhere针对实现IEnumerable.

于 2013-06-23T16:08:37.323 回答
1

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

于 2013-06-23T16:09:01.060 回答