我想使用LINQ to SQL根據數組中的值查詢相應的數據,但遇到問題。我讀這些線程,但我不知道他們是否實際上是我想要做的,如果他們是如何實現它們:如何爲數組中的每個條目運行查詢?
LINQ equivalent of foreach for IEnumerable<T>
我有我的數組字符串的行[]',我怎樣才能爲每個條目運行以下查詢,並以一種允許我以一致的方式輸出它們的方式存儲結果。 再次這裏是我的數組的一個示例: 例子: 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);
}
列表創建是好的,我也想在寫列表將是確定的,但我不能弄清楚如何爲每個物品運行查詢int array?
我的數組中的每個條目都以Z開頭,然後包含字母數字字符的任何排列(如果它很重要)。示例:Z3333
數組中的每個條目都對應於數據庫'SpecificTable'中表中的條目。我想在該表中返回與此值相關的所有內容,以便我可以輸出該數據的詳細信息。
例子: 我想quering Z1234開始,當Z1234在「SpecificTable」發現我希望能夠輸出各種細節是這樣的:
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());
}
通過使用循環希望能夠根據初始數組創建所有結果的列表,並輸出其相應的各種值,如'說明'。
如果這還不夠信息,請讓我知道我可以提供更清楚。
'foreach(var result in thisQuery){results.Add(result); }'可縮短爲'results = thisQuery.ToList();' – nothrow