无法访问IQueryable<;T>;按索引
本文关键字:索引 gt lt 访问 IQueryable | 更新日期: 2023-09-27 18:19:32
我是c#的新手。我正试图使用linq查询生成的列表(?)在订购单上构建一个表。其中一部分涉及引用该列表中的特定项目,如下所示:
using (CellOrderingDBEntities db = new CellOrderingDBEntities())
{
var AccessoryItems = from AccessoryDescription in db.Accessories
join HardwareID in db.HardwareTypes
on AccessoryDescription.HardwareType equals HardwareID.HardwareID
where AccessoryDescription.DateRetired == null
select AccessoryDescription;
List<Device> DevicesList = (List<Device>)Session["DevicesList"];
Guid AccessoriesOrder = (from ServiceID in db.TypeOfServices
where ServiceID.ServiceType == "Accessories Order"
select ServiceID.ServiceID).FirstOrDefault();
//This Int is used to build the accessories table
int AccessoryRows = AccessoryItems.Count();
if (SType == AccessoriesOrder)
{
for (int r = 0; r <= AccessoryRows; r++)
{
TableRow row = new TableRow();
for (int c = 0; c < 3; c++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
int ai = 0;
int ri = 0;
tb.ID = "TextBoxRow_" + r + "Col_" + c;
if (c == 0)
{
cell.Controls.Add(tb);
}
else if (c == 1)
{
cell.Text = AccessoryItems[ai];
ai++;
}
}
}
}
我被这个错误卡住了:
无法将带有[]的索引应用于类型为"System.Linq.IQueryable<数据.附件>'
我曾尝试将其转换为字符串列表,但由于某些原因,我不明白为什么它不允许我通过索引访问IQueryable
。
您可以使用ElementAt访问序列中特定点的元素。因此,替换:
cell.Text = AccessoryItems[ai];
带有
cell.Text = AccessoryItems.ElementAt(ai) as String;
想好了:
var AccessoryItems = from AccessoryDescription in db.Accessories
join HardwareID in db.HardwareTypes
on AccessoryDescription.HardwareType equals HardwareID.HardwareID
where AccessoryDescription.DateRetired == null
select AccessoryDescription;
List<Accessory> AIL = AccessoryItems.ToList();
在for循环中:
cell.Text = (string) AIL[ai].AccessoryDescription;
它起作用了!