How to view LINQ to SQL Query results in Messagebox?

a 夏天 提交于 2019-12-25 07:23:34

问题


I am running a query with 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?

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:

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.


回答1:


MessageBox.Show can show a string as a message, your query returns tabular data. You can concatenate all the results of query and then show it in MessageBox like:

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.



来源:https://stackoverflow.com/questions/18965426/how-to-view-linq-to-sql-query-results-in-messagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!