如何在模型中检索列表?
这就是我正在尝试的:
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&gt;'不包含'ToList'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.ToList(System.Linq.ParallelQuery)'有一些无效的参数
错误2实例参数:无法从'System.Linq.IQueryable&gt;'转换到'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);
}
}
}
有没有更好的方法来编写该查询,或者我的方式是否正确?
答案 0 :(得分:6)
不知道为什么你会得到ParallelQuery
- 除非你的源文件中有一些古怪的using
。
在任何情况下,您似乎都有可枚举的枚举 - 请尝试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
然后转为阵列/列表。
答案 1 :(得分:2)
您的问题是Player.Records
是List<Record>
,而您从查询中获得了IEnumerable<List<Record>>
(即0到多个玩家的记录),因此.ToList()
可以获得一个List<List<Record>>
。如果有多个具有相同名称的玩家并且您希望它从所有玩家那里收集记录,请使用Andras Zoltan的解决方案。如果您想确保(通过抛出异常,如果有0或超过1个结果)确切一个玩家具有给定名称,并且仅返回他的记录,请使用以下解决方案之一:(关键更改为{{3} } - 另请查看.Single()
,了解它是否更符合您的需求)
//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();
答案 2 :(得分:0)
如果你改变了
List<Record> records = new List<Record>();
到
var records = new List<List<Record>>();
有用吗?如果播放器具有记录列表,则看起来您的查询正在返回记录列表的列表。
编辑: 在那里,修复了返回列表...无论哪种方式,这可能不是您正在寻找的解决方案,只需突出显示问题所在。
答案 3 :(得分:0)
您可以尝试重构您的查询
records = context.Players.First(player => player.Name == selectedPlayer).Records.ToList();