C# 如何迭代 linq-to-sql 查询结果的结果并附加结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10170316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:43:18  来源:igfitidea点击:

How to iterate over results of a linq-to-sql query result and append the results?

c#asp.netsql-serverlinqlinq-to-sql

提问by codewarrior

I'm new to C# and Linq-to-Sql.

我是 C# 和 Linq-to-Sql 的新手。

I've a table 'InstrumentTypes' of this form:

我有一个这种形式的表“InstrumentTypes”:

typeId(int)  | type(varchar)  |  subttype(varchar)

101               Keys           keyboard
102               Keys           accessories
103               Guitar         acoustic
104               Guitar         electric

I need to fetch all 'typeId's from the table based on a search by 'type' as input, and all the typeId's needed to be bound to a ASP Repeater.

我需要根据“type”作为输入进行搜索,从表中获取所有“typeId”,并且需要将所有 typeId 绑定到 ASP 中继器。

So far I've written the following code:

到目前为止,我已经编写了以下代码:

// 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
}

I'm unable to figure out how to iterate over the results from the query, store them in a datastructure and bind them to a Repeater.

我无法弄清楚如何迭代查询的结果,将它们存储在数据结构中并将它们绑定到中继器。

The following code binds it to the Repeater:

以下代码将其绑定到中继器:

Repeater1.DataSource= //name of data structure used to store the types goes here
Repeater1.DataBind();

Could anyone please help me out?

有人可以帮我吗?

EDIT: For each typeID obtained, I want to access another table 'Instruments' and retrieve all Instruments belonging to that typeId. The table 'Instruments' is like this:

编辑:对于获得的每个 typeID,我想访问另一个表“Instruments”并检索属于该 typeId 的所有仪器。“仪器”表是这样的:

instrumentId     typeID    name     description
1000             101       yamaha   xyz

Based on Arialdo's answer, I'm doing this:

根据 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();

But I get a compilation error saying 'The best overloaded method match for the List has some invalid arguments'. Where am I going wrong?

但是我收到一个编译错误,提示“列表的最佳重载方法匹配有一些无效参数”。我哪里错了?

采纳答案by Arialdo Martini

What you get from

你从中得到什么

var type = (from m in database.InstrumentTypes
        where m.type == requestType
        select m);

is a collection of InstrumentTypes, not a collection of ids.

是 的集合InstrumentTypes,而不是 id 的集合。

This works for me

这对我有用

var types = (from m in database.InstrumentTypes
        where m.type == requestType
        select m);
var ids = new List<int>();
foreach (var type in types)
{
    ids.Add(type.Id);
}

which you could easily convert to

您可以轻松转换为

var ids = (from m in database.InstrumentTypes
        where m.type == requestType
        select m.Id).ToList();

[Edit]

[编辑]

You can directly query your instruments and navigate to related objects, as long as you defined a relationship between InstrumentTypeand Instrument.

您可以直接查询您的工具并导航到相关对象,只要您定义了InstrumentType和之间的关系Instrument

var instruments = (from i in database.Instrument
                      where i.InstrumentType.type == requestType
                      select i);

No need to have separate foreaches or queries. The i.InstrumentTypewill convert to a join, as you could verify with a SQL profiler

无需单独的foreaches 或查询。在i.InstrumentType将转换为join,因为你可以用SQL事件探查器验证

回答by bkingdev

I am not sure what you are asking.

我不确定你在问什么。

With out explicitly defining the returned type of your query you are already returning an IEnumerable<InstrumentTypes> object. If you want a list of ID's you can simply refine your query to return ID's rather than a list of InstrumentTypes. Of course, then you'd be returning an IEnumerable<int> object.

如果没有明确定义查询的返回类型,您就已经返回了一个 IEnumerable<InstrumentTypes> 对象。如果您想要一个 ID 列表,您可以简单地优化您的查询以返回 ID 而不是 InstrumentTypes 列表。当然,那么您将返回一个 IEnumerable<int> 对象。

var type = (from m in database.InstrumentTypes
        where m.type == requestType
        select m.typeId);