我是C#和Linq-to-Sql的新手。如何遍历linq-to-sql查询结果的结果并追加结果?
我有一个表这种形式的“InstrumentTypes”:
typeId(int) | type(varchar) | subttype(varchar)
101 Keys keyboard
102 Keys accessories
103 Guitar acoustic
104 Guitar electric
我需要获取类型‘输入所有“从表基于由搜索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,我想访问另一个表'Instruments'并检索属于该typeId的所有工具。 表“仪器”是这样的:
instrumentId typeID name description
1000 101 yamaha xyz
基于阿里尔多的答案,我这样做:
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 2012-04-16 07:47:28
@SkonJeet:我已经更新了上面的问题。 – codewarrior 2012-04-16 08:30:56