【发布时间】:2013-09-20 22:54:12
【问题描述】:
我正在尝试使用 LINQ to SQL 根据数组中的值查询相应的数据,但遇到了问题。我正在阅读这些线程,但不确定它们是否真的是我想要做的,以及它们是否是如何实现它们的:
LINQ equivalent of foreach for IEnumerable<T>
我有我的字符串数组'lines[]',我如何为每个条目运行以下查询,并以允许我以连贯方式输出它们的方式存储结果。 这里又是我的数组的一个示例: 例子: Z1234 Z2345 ZAF38383
//some non working code
List<string> results = new List<string>();
var thisQuery = from c in myContext.SpecificTable
where c.itemNumber == (foreach (string i in lines))
select c;
foreach (var result in thisQuery)
{
results.Add(result);
}
列表创建很好,我认为写入列表也可以,但我不知道如何为数组中的每个项目运行查询?
我的数组中的每个条目都以 Z 开头,然后将包含任何重要的字母数字字符排列。示例:Z3333
数组中的每个条目对应于我的数据库“SpecificTable”中的表中的一个条目。我想在该表中返回与该值有关的所有内容,然后我可以输出该数据的详细信息。
示例: 我想从查询 Z1234 开始,当在“SpecificTable”中找到 Z1234 时,我希望能够像这样输出各种详细信息:
foreach (var res in thisQuery)
{
//each result from the query (total of 3 from the example) will now show their Description in a messagebox.
MessageBox.Show("Description:" + res.Description.ToString());
}
通过使用循环,我希望能够根据初始数组创建所有结果的列表,并输出它们对应的各种值,例如'Description'。
如果这仍然不够信息,请让我知道我可以提供什么更清楚。
【问题讨论】:
-
foreach (var result in thisQuery) { results.Add(result); }可以简写为results = thisQuery.ToList();