2013-09-01 63 views
1

我是一名在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()

+1

Console.WriteLine(response.Name);也許? – rene

+0

'Console.WriteLine(response)'打印'Response.ToString()',它是'Response'的類型名稱。我認爲你需要打印你的'response'對象的一個​​屬性*。 –

+0

@rene,這將只返回名稱值,但我希望打印出所有字段的值。有什麼建議麼? –

回答

2

您可以使用reflection來做到這一點。確保您使用的是 System.Reflection。

static void Main(string[] args) 
{ 
    using (AcumenProjectsDBDataContext acumenResponse = new AcumenProjectsDBDataContext()) 
    { 
    IEnumerable<Response> responseData = from response in acumenResponse.Responses select response; 
    //added code 
    foreach (Response response in responseData) 
    { 
     foreach (PropertyInfo prop in response.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) 
     { 
      object value = prop.GetValue(response, new object[] { }); 
      Console.WriteLine("{0} = {1}", prop.Name, value); 
     } 
     } 
     Console.ReadKey(); 
    } 

} 
+0

我會這樣做的類型的ToString覆蓋,否則它是一次性使用。 –

+0

這個答案確實得到了期望的輸出,但就像零售商說的那樣,這可能有點矯枉過正。非常感謝大家的指導。 –

0

直到我們知道Response是由什麼組成並打算從中打印出來的,我們無法給出明確的答案,但是如果您想保持您的Console.WriteLine調用完全按照原樣進行,則應該覆蓋Response類中的ToString()方法返回要顯示的字符串。

public override string ToString() 
{ 
    return string.Format("Property1: {0}\nProperty2: {1}\nProperty3: {2}", 
         this.Property1, this.Property2, this.Property3); 
} 

這是因爲Console.WriteLine將隱式調用一個類型,這不是一個stringToString方法,以及ToString默認的實現只返回類型的名稱。


這應該工作:

public class Response 
{ 
    ... 

    public override string ToString() 
    { 
     return string.Format("ResponseID: {0}\nStatusID: {1}\nTitle: {2}\nProjectID: {3}\nSourceID: {4}\nSourceTitle: {5}\n...", 
      ResponseID, StatusID, Title, ProjectID, SourceID, SourceTitle); 
     // no need to call .ToString() on integer properties here, it's called implicitly anyway 
    } 
} 

輸出爲Console.WriteLine(Response);

ResponseID: 1 
StatusID: 123 
Title: This is the title 
ProjectID: 1 
SourceID: 456 
SourceTitle: This is the source title 

在你ToString覆蓋,你可以精確指定如何希望每個屬性出現,例如{0:C2}會打印1$1.00。您也可以使用\t(選項卡)排列輸出。

+0

那麼你只會這樣做一次,ToString告訴系統你的類應該如何呈現自己作爲一個字符串。另一種方法是使用反射這imho有點矯枉過正。 –

+0

會dataGridView.DataSource = dc.GetTable 在這種情況下工作?如果是,那我怎麼用它來打印出結果呢? –

+0

您可以使用您在問題中發佈的完全相同的代碼! :) –

1

看看foreach循環中「response」變量的類型。這是LinqConsoleApplication.Response。當一個對象傳遞給Console.WriteLine(object)方法時,ToString()方法被稱爲該對象。如果調用對象的ToString()方法而不明確覆蓋對象並實現自定義函數所有,那麼默認結果是您將整個對象類型作爲字符串獲取,例如"LinqConsoleApplication.Response"

你需要做的是什麼,而在foreach循環迭代,以創建一個由在你感興趣的對象的屬性的串聯創建一個自定義字符串

,例如:

foreach (Response response in responseData) 
      { 
       string responseString = response.SomeProperty1.ToString() + " " + response.SomeProperty2.ToString(); 
       Console.WriteLine(responseString); 
      } 
+0

你可能想編輯你的答案,以實際*使用*你正在建立的字符串。否則,輸出保持不變... –

+0

這就是我現在所做的,但是當一個表格包含超過15個字段時,我該怎麼辦?我將顯式轉換每個字段toString()並打印它們?這將是很多代碼的寫作,對吧? LINQ中沒有DataTable概念可以從表中返回所有數據(對於所有字段)? –

+0

這種方式涉及到一次性使用的字符串,每次需要將類型呈現爲字符串時,您都必須重新鍵入所有內容(或複製/粘貼)。 –