gpt4 book ai didi

c# - 如何从 'System.Linq.IQueryable 转换为 List

转载 作者:行者123 更新时间:2023-11-30 19:44:17 30 4
gpt4 key购买 nike

如何检索模型中的列表?

这就是我正在尝试的:

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

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

最佳答案

不知道您为什么会得到 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() 来获取单个玩家 - whose Records 你然后变成一个数组/列表。

关于c# - 如何从 'System.Linq.IQueryable<System.Collections.Generic.List<Model.Record> 转换为 List<Record>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781949/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com