我是一名在LINQ中編程的初學者,我想知道如何從控制檯應用程序中使用LINQ從表格(在SQL Server中)打印所有數據。到目前爲止,我所做的工作是創建一個名爲Response的表,它具有多個字段(我在SQL Server Management Studio中設計了表),並編寫了一個控制檯C#類來打印所有值。這裏是我的代碼爲:用LINQ打印表格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (DatabaseDBDataContext responses = new DatabaseDBDataContext())
{
IEnumerable<Response> responses = from response in responses.Responses select response;
foreach (Response response in responses)
{
Console.WriteLine(response);
}
Console.ReadKey();
}
}
}
}
然而,當我在cmd中運行它,我得到這個作爲我的輸出:
LinqConsoleApplication.Response
LinqConsoleApplication.Response
從google搜索一些解決方案,我發現Console.WriteLine(response)
應該從表中返回EVERYTHING(select *),但似乎並非如此。有什麼建議嗎?在我構建查詢的方式中是否有錯誤?我是否需要使用StringBuilder方法將每個字段附加到writeLine()
?
Console.WriteLine(response.Name);也許? – rene
'Console.WriteLine(response)'打印'Response.ToString()',它是'Response'的類型名稱。我認爲你需要打印你的'response'對象的一個屬性*。 –
@rene,這將只返回名稱值,但我希望打印出所有字段的值。有什麼建議麼? –