2

如何检索模型中的列表?

这就是我正在尝试的:

private void cbxPlayers_SelectedValueChanged(object sender, EventArgs e)
        {
            List<Record> records = new List<Record>();
            string selectedPlayer = cbxPlayers.SelectedItem.ToString();
            using (ProgressRecordContext context = new ProgressRecordContext())
            {
                records = (from Player in context.Players
                          where Player.Name == selectedPlayer
                          select Player.Records).ToList<Record>();
            }
        }

但是,这不起作用,我错过了什么?

这些是需要时的模型:

public class Player
    {
        [Key][DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int AccountNumberId { get; set; }

        public string Name { get; set; }

        public virtual List<Record> Records { get; set; }
    }

public class Record
    {
        public int RecordId { get; set; }
        public int AccountNumberId { get; set; }

        public double Level { get; set; }
        public int Economy { get; set; }
        public int Fleet { get; set; }
        public int Technology { get; set; }
        public int Experience { get; set; }

        public DateTime TimeStamp { get; set; }

        public virtual Player Player { get; set; }
    }

编辑:这是错误消息:

错误 1“System.Linq.IQueryable>”不包含“ToList”的定义,并且最佳扩展方法重载“System.Linq.ParallelEnumerable.ToList(System.Linq.ParallelQuery)”有一些无效参数

错误 2 实例参数:无法从“System.Linq.IQueryable>”转换为“System.Linq.ParallelQuery”

编辑:

我看到我可能不太清楚我想要做什么。我最终想出了一种方法来做我想做的事,这就是:

private void cbxPlayers_SelectedValueChanged(object sender, EventArgs e)
        {
            lstvRecords.Items.Clear();
            if(cbxPlayers.SelectedIndex == -1)
            {
                return;
            }
            string selectedPlayer = cbxPlayers.SelectedItem.ToString();
            using (ProgressRecordContext context = new ProgressRecordContext())
            {
                var records = from Player in context.Players
                          from Record in context.Records
                          where Player.Name == selectedPlayer &&
                          Player.AccountNumberId == Record.AccountNumberId
                          select new
                          {
                              Level = Record.Level,
                              Economy = Record.Economy,
                              Fleet = Record.Fleet,
                              Technology = Record.Technology,
                              Experience = Record.Experience,
                              TimeStamp = Record.TimeStamp
                          };

                foreach (var element in records)
                {
                    string[] elements = {element.Level.ToString(),
                                            element.Economy.ToString(),
                                            element.Fleet.ToString(),
                                            element.Technology.ToString(),
                                            element.Experience.ToString(),
                                            element.TimeStamp.ToString()
                                        };
                    ListViewItem lvi = new ListViewItem(elements);
                    lstvRecords.Items.Add(lvi);
                }
            }
        }

有没有更好的方法来编写该查询,或者我这样做的方式是否正确?

4

4 回答 4

6

不知道你为什么会得到ParallelQuery- 除非你的源文件中有一些古怪using的 s 。

在任何情况下,您似乎都有一个可枚举的枚举 - 尝试SelectMany(请注意,您也需要using System.Linq;将此作为扩展方法):

records = (from Player in context.Players 
           where Player.Name == selectedPlayer 
           select Player.Records).SelectMany(r => r).ToList();

另外-除非您打算在该列表中添加/删除,否则您应该只使用一个数组,即使用.ToArray().

正如@Tim S (+1) 所指出的那样-如果您希望这里只有一个玩家,那么您应该使用SingleOrDefault()该玩家来获取单个玩家-Records然后您将其变成一个数组/列表。

于 2012-10-08T12:43:10.290 回答
2

您的问题是Player.Recordsa List<Record>,并且您IEnumerable<List<Record>>从查询中得到一个(即 0 到许多玩家的记录),所以.ToList()得到一个List<List<Record>>. 如果有多个同名玩家并且您希望它收集所有玩家的记录,请使用 Andras Zoltan 的解决方案。如果您想确保(通过在有 0 个或多于 1 个结果时抛出异常)只有一个玩家具有给定名称,并且只返回他的记录,请使用以下解决方案之一:(key change being .Single()- also take a看看SingleOrDefault它是否更适合您的需求)

//I prefer this solution for its conciseness and clarity.
records = context.Players.Single(Player => Player.Name == selectedPlayer).Records;

//if you'd like to use the LINQ query format, I'd recommend this.
records = (from Player in context.Players
          where Player.Name == selectedPlayer
          select Player).Single().Records;

//this is more similar to your original query.
records = (from Player in context.Players
          where Player.Name == selectedPlayer
          select Player.Records).Single().ToList();
于 2012-10-08T13:18:57.453 回答
0

如果你改变

     List<Record> records = new List<Record>();

     var records = new List<List<Record>>();

它有效吗?如果播放器有一个记录列表,看起来您的查询正在返回一个记录列表列表。

编辑:在那里,修复了返回列表......无论哪种方式,这可能都不是您正在寻找的解决方案,只是突出问题所在。

于 2012-10-08T12:45:05.407 回答
0

您可以尝试重构您的查询

records = context.Players.First(player => player.Name == selectedPlayer).Records.ToList();
于 2012-10-08T14:37:35.677 回答