簡體   English   中英

如何在Messagebox中查看LINQ to SQL Query結果?

[英]How to view LINQ to SQL Query results in Messagebox?

我正在使用LINQ to SQL運行查詢。 我試圖將結果輸出到消息框中,但顯示的消息框是顯示我的查詢而不是結果?

這是我的代碼

var thisQuery = from c in myContext.SpecificTable
                where c.UniqueValue == "\'Z1234\'"
                select new
                {
                c.UniqueValue,
                c.UniqueValueDetails,
                c.UniqueValueType
                };

MessageBox.Show(thisQuery.ToString());

我想問題是我不能直接將thisQuery調用成字符串,但我不知道如何查看我的查詢結果呢?

當我運行上面的內容時,我得到一個結果消息框,顯示:

SELECT [t0].[UniqueValue], [t0].[UniqueValueDetails], [t0].[UniqueValueType]

FROM [dbo].[SpecificTable] AS [t0]

WHERE [t0].[UniqueValue] = @p0

如何在消息框中查看查詢結果?

我也試過存儲整個查詢結果tostring但最終結果相同:

var thisQuery = (from c in myContext.SpecificTable
                where c.UniqueValue == "\'Z1234\'"
                select new
                {
                c.UniqueValue,
                c.UniqueValueDetails,
                c.UniqueValueType
                }).ToString();

MessageBox.Show(thisQuery);

我試着查看這個並閱讀了一些線程,但我似乎無法用能夠產生結果的方式來表達問題。

MessageBox.Show可以將字符串顯示為消息,您的查詢返回表格數據。 您可以連接查詢的所有結果,然后在MessageBox顯示它:

var messageString = string.Join(Environment.NewLine, 
                               thisQuery.Select(r=> string.Format("{0}, {1}, {2}"
                                                    , r.UniqueValue
                                                    , r.UniqueValueDetails
                                                    ,r.UniqueValueType));

MessageBox.Show(messageString);

如果使用Grid來顯示查詢記錄,則會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM