Reputation: 547
I am a starter with programming in LINQ and I would like to know how I can print all the data from a table (in SQL Server) using LINQ from a console application. What I have done till now is to create a table called Response which has several fields (I have designed the table in SQL Server Management Studio) and have written a console C# class to print all the values out. Here's my code for that:
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();
}
}
}
}
However, when I run this in cmd, I get this as my output:
LinqConsoleApplication.Response
LinqConsoleApplication.Response
From googling for some solutions, I have found that Console.WriteLine(response)
should return EVERYTHING (select *) from a table, but that doesn't seem to be the case. Any suggestions please? Is there an error in the way I have framed the query? Will I need to use a StringBuilder method to append each and every field to the writeLine()
?
Upvotes: 1
Views: 2883
Reputation: 71187
We can't give you a definitive answer until we know what Response
is made of and what you'd like to print out of it, but if you want to keep your Console.WriteLine
call exactly as it is, you should override the ToString()
method in your Response
class, to return the string you'd like to see printed.
public override string ToString()
{
return string.Format("Property1: {0}\nProperty2: {1}\nProperty3: {2}",
this.Property1, this.Property2, this.Property3);
}
That's because Console.WriteLine
will implicitly call the ToString
method on a type that's not a string
, and the default implementation of ToString
simply returns the type's name.
This should work:
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
}
}
Output for Console.WriteLine(Response);
ResponseID: 1
StatusID: 123
Title: This is the title
ProjectID: 1
SourceID: 456
SourceTitle: This is the source title
In your ToString
override you can specify exactly how you want each property to appear, for example {0:C2}
would print 1
as $1.00
. Also you might want to use \t
(tab) to line up the output.
Upvotes: 0
Reputation: 5802
You can do this with using reflection.Make sure you're using 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();
}
}
Upvotes: 2
Reputation: 123
Look at the type of the "response" variable in your foreach loop. It's LinqConsoleApplication.Response
. When an object is passed to the Console.WriteLine(object)
method the ToString()
method is called of that object. When you call the ToString()
method of an object without explicitly overriding it and implementing a custom functionallity, then the default result is that you get the full object type as a string e.g. "LinqConsoleApplication.Response"
.
What you need to do is while iterating in the foreach loop to create a custom string which is created by a concatenation of the properties of the object in which you're interested in.
For example:
foreach (Response response in responseData)
{
string responseString = response.SomeProperty1.ToString() + " " + response.SomeProperty2.ToString();
Console.WriteLine(responseString);
}
Upvotes: 1