简体   繁体   English

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

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

I am running a query with LINQ to SQL. 我正在使用LINQ to SQL运行查询。 I am trying to output the results into a messagebox, but the messagebox that shows up is displaying the my query rather than the results? 我试图将结果输出到消息框中,但显示的消息框是显示我的查询而不是结果?

Here is my code 这是我的代码

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

MessageBox.Show(thisQuery.ToString());

I imagine the issue is I can't call thisQuery into a string directly, but I'm not sure how to view the results of my query otherwise? 我想问题是我不能直接将thisQuery调用成字符串,但我不知道如何查看我的查询结果呢?

When I run the above I get a resulting message box that shows: 当我运行上面的内容时,我得到一个结果消息框,显示:

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

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

WHERE [t0].[UniqueValue] = @p0

How can I view my results from the query in the message box? 如何在消息框中查看查询结果?

I also tried storing the entire query result tostring but ended up with the same results: 我也试过存储整个查询结果tostring但最终结果相同:

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

MessageBox.Show(thisQuery);

I tried looking this up and have read a handful of threads, but I can't seem to word the question in a way that has yielded results. 我试着查看这个并阅读了一些线程,但我似乎无法用能够产生结果的方式来表达问题。

MessageBox.Show can show a string as a message, your query returns tabular data. MessageBox.Show可以将字符串显示为消息,您的查询返回表格数据。 You can concatenate all the results of query and then show it in MessageBox like: 您可以连接查询的所有结果,然后在MessageBox显示它:

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

MessageBox.Show(messageString);

Its better if you use Grid for displaying records from query. 如果使用Grid来显示查询记录,则会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM