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

时间:2013-06-23 15:37:28

标签: c# asp.net sql arrays collections

我有以下课程:

    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;

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试对Where类使用players扩展方法。 Where针对实现IEnumerable的集合进行操作。

答案 1 :(得分:1)

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