【发布时间】:2012-04-16 07:42:16
【问题描述】:
我是 C# 和 Linq-to-Sql 的新手。
我有一个这种形式的表“InstrumentTypes”:
typeId(int) | type(varchar) | subttype(varchar)
101 Keys keyboard
102 Keys accessories
103 Guitar acoustic
104 Guitar electric
我需要根据“type”作为输入的搜索从表中获取所有“typeId”,并且所有 typeId 都需要绑定到 ASP 中继器。
到目前为止,我已经编写了以下代码:
// requestType contains the type from the search
var type = (from m in database.InstrumentTypes
where m.type == requestType
select m);
foreach(var typeId in type)
{
//code
}
我无法弄清楚如何迭代查询的结果,将它们存储在数据结构中并将它们绑定到中继器。
以下代码将其绑定到Repeater:
Repeater1.DataSource= //name of data structure used to store the types goes here
Repeater1.DataBind();
谁能帮帮我?
编辑: 对于获得的每个 typeID,我想访问另一个表“仪器”并检索属于该 typeId 的所有仪器。 表“仪器”是这样的:
instrumentId typeID name description
1000 101 yamaha xyz
根据 Arialdo 的回答,我正在这样做:
var type = (from m in database.InstrumentTypes
where m.type == requestType
select m);
var instruments = new List<Instrument>();
foreach (var i in type)
{
instruments.Add(from x in database.Instruments
where x.typeId == i.typeId
select x);
}
Repeater1.DataSource = instruments;
Repeater1.DataBind();
但我收到一个编译错误,提示“列表的最佳重载方法匹配有一些无效参数”。我哪里错了?
【问题讨论】:
-
"遍历查询结果" - 为什么要循环遍历结果?什么样的数据结构?
-
@SkonJeet:我已经更新了我上面的问题。
标签: c# asp.net sql-server linq linq-to-sql