我是新来的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
。
如果有人知道围绕类型包装我的头的好资源,我认为这是我没有得到的,因为我一直在抨击我的头,我会接受它。非常感谢! – Ischade
您可能会发现使用以下语法会更容易:'foreach(AccessoryItems中的var ai)'而不是this:'for(int r = 0; r <= AccessoryRows; r ++)'如果您不需要'r'除了编制索引'AccessoryItems' – meataxe
就好的LINQ资源而言:下载LINQPad(http://www.linqpad.net/)并随身携带。 (我也可以推荐使用J&B Albahari的C#4.0/5.0)。另外,我建议您了解本地(参见IEnumerable)和解释查询(参见IQueryable )和LINQ的延期执行模式。一旦你理解了这些概念,事情就会变得更加清晰。 –
afrischke