问题描述
我是 LINQ 编程的初学者,我想知道如何使用控制台应用程序中的 LINQ 打印表(在 SQL Server 中)中的所有数据.到目前为止,我所做的是创建一个名为 Response 的表,该表具有多个字段(我在 SQL Server Management Studio 中设计了该表)并编写了一个控制台 C# 类来打印所有值.这是我的代码:
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
通过谷歌搜索某些解决方案,我发现 Console.WriteLine(response) 应该从表中返回 EVERYTHING (select *),但似乎并非如此.请问有什么建议吗?我构建查询的方式是否有错误?我需要使用 StringBuilder 方法将每个字段附加到 writeLine() 吗?
推荐答案
你可以使用反射来做到这一点.确保你正在使用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(); } }
问题描述
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()?
推荐答案
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(); } }