gpt4 book ai didi

c# - 无法按索引访问 IQueryable 的项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:22 24 4
gpt4 key购买 nike

我是 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++;
}
}
}
}

虽然我遇到了这个错误:

Cannot apply indexing with [] to an expression of type 'System.Linq.IQueryable<Data.Accessory>'

我已经尝试将它转换为字符串列表,但由于我不明白为什么它不允许我通过索引访问 IQueryable 的原因。

最佳答案

你可以使用 ElementAt访问序列中特定点的元素。所以,替换:

cell.Text = AccessoryItems[ai];

cell.Text = AccessoryItems.ElementAt(ai) as String;

关于c# - 无法按索引访问 IQueryable<T> 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17266568/

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