2013-09-20 80 views
1

我想使用LINQ to SQL根據數組中的值查詢相應的數據,但遇到問題。我讀這些線程,但我不知道他們是否實際上是我想要做的,如果他們是如何實現它們:如何爲數組中的每個條目運行查詢?

LINQ equivalent of foreach for IEnumerable<T>

Linq style "For Each"

我有我的數組字符串的行[]',我怎樣才能爲每個條目運行以下查詢,並以一種允許我以一致的方式輸出它們的方式存儲結果。 再次這裏是我的數組的一個示例: 例子: 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()); 
} 

通過使用循環希望能夠根據初始數組創建所有結果的列表,並輸出其相應的各種值,如'說明'。

如果這還不夠信息,請讓我知道我可以提供更清楚。

+2

'foreach(var result in thisQuery){results.Add(result); }'可縮短爲'results = thisQuery.ToList();' – nothrow

回答

1

嘗試:

這將循環在myContext.SpecificTable行,並創建一個innerloop,以檢查是否有行內的匹配。

var thisQuery = from c in myContext.SpecificTable 
       from i in lines 
       where c.itemNumber == i 
       select c; 

或本:

這也將這樣做,只有第二「迭代」是內包含完成。

var thisQuery = from c in myContext.SpecificTable 
       where lines.Contains(c.itemNumber) 
       select c; 

我認爲最好把db查詢放在outerloop中,因爲你不想爲行中的每個項目使用'tablescan'。

所以它會做一個'桌子掃描'只有一個,並嘗試找到一個匹配的行。

我想,如果你嘗試這樣就會加速:

var linesHashSet = new HashSet<string>(lines); 

var thisQuery = from c in myContext.SpecificTable 
       where linesHashSet.Contains(c.itemNumber) 
       select c; 

這將在第二次迭代使用一個HashSet(索引列表)


更新:

有可能迭代客戶端的行,如下所示:

var thisQuery = from c in myContext.SpecificTable.ToArray() 
       where lines.Contains(c.itemNumber) 
       select c; 

但這會有一個性能差。 我建議你嘗試中間的一個。

+0

當我嘗試第一次查詢時,它返回以下錯誤:本地序列不能用於除Contains運算符之外的查詢運算符的LINQ to SQL實現。我真的不知道那是怎麼回事。 我得到的最後一個例子,至少沒有錯誤,當我用line'where linesHashSet.ToList()。Contains ....但我後來調用查看thisQuery的內容不產生任何結果,我想象意味着我的查詢實際上沒有選擇任何東西? –

+0

'本地序列不能用於查詢運算符的LINQ to SQL實現,但Contains運算符除外.''表示它想要爲整個linq語句創建一個查詢,但不能將'from i in lines'轉換爲sql語句。我建議你使用中間的一個。是否有可能監視生成的sql語句?我會添加一些東西給我的答案。 –

+0

關於空洞的結果,我不知道數據是怎麼樣的,所以我無法預測結果。 –

1

不知道我是否正確理解您的問題,但是您想要使用「lines」數組加入SpecificTable中的條目,並將結果輸出爲列表?

var results = myContext.SpecificTable.Select(c => lines.Contains(c.ItemNumber)).ToList(); 
foreach(var result in results) 
{ 
    Console.WriteLine(string.format("{0}:{1}", "Description", result.Description)); 
} 

如果您只想要特定屬性,則可以返回一個匿名對象列表。

var results = myContext.SpecificTable.Where(c => lines.Contains(c.ItemNumber)).Select(c => new {c.ItemNumber, c.Description}).ToList(); 
foreach(var result in results) 
{ 
    Console.WriteLine(string.format("{0}:{1}", "Description", result.Description)); 
} 
+0

如果'結果'已經放入列表中,這是不是無法處理'result.Description'的最後一次調用?在我的項目中試用它並不能識別result.Description,我想知道這是爲什麼? –

+0

'結果'將是類型IEnumerable >我猜。找出最簡單的方法是放置一個斷點,並找到Select查詢的返回類型。本質上,如果單個元素的類型不是你想要的,你可以提取特定的屬性。你'SpecificTable'對象是否有一個Description屬性? – tranceporter

相關問題