IEnumerable 和 console.writeline
IEnumerable and console.writeline
我有这个简单的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Entity;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
NuLabsEntities db = new NuLabsEntities();
IEnumerable<company> companies = from cmp in db.company select cmp;
foreach (var row in companies)
{
Console.WriteLine(companies);
Console.ReadLine();
}
}
}
}
我知道这是一个基本问题:我正在学习 c#
但我不明白为什么,在使用 ado.net 创建一个 edmx 文件并尝试 运行 这个简单的代码后,它 returns 我得到以下查询而不是 a 的结果公司的行列表 table:
SELECT
[Extent1].[companyId] AS [companyId],
[Extent1].[labirintoCodiceCliente] AS [labirintoCodiceCliente],
[Extent1].[labirintoCodiceAteco2007] AS [labirintoCodiceAteco2007],
[Extent1].[name] AS [name],
[Extent1].[doc] AS [doc],
[Extent1].[notes] AS [notes],
[Extent1].[vatNumber] AS [vatNumber],
[Extent1].[taxCode] AS [taxCode],
[Extent1].[LabirintoFornitoreId] AS [LabirintoFornitoreId],
[Extent1].[LabirintoCommercialistaId] AS [LabirintoCommercialistaId],
[Extent1].[LabirintoConsulenteDelLavoroId] AS [LabirintoConsulenteDelLavoroId]
FROM [dbo].[company] AS [Extent1]
Console.WriteLine(companies);
应该是 Console.WriteLine(row.blah);
您需要调用 .ToList()
然后循环遍历集合。当您调用 ToList()
.
时会评估查询
使用您编码的 foreach
,您将每个 company
放入行中。您可以从 row
.
访问 company
的属性
让我们假设您公司的结构是这样的
public class company
{
public int companyId {get;set;}
public string companyName {get;set;}
}
你的代码应该是
foreach (var row in companies.ToList())
{
Console.WriteLine("CompanyId:"+row.CompanyId.ToString());
Console.WriteLine("CompanyName:"+row.CompanyName);
Console.ReadLine();
}
我认为你应该传递行对象
Console.WriteLine(row);
为什么?
这是因为公司类型是 System.Data.Entity.Infrastructure.DbQuery<Company>
并且它的 ToString()
方法是 returns 查询。
当你使用Console.WriteLine(somthings)
时,somethings的ToString
方法将被用于输出数据,因此你将收到ToString
结果,即Query Text。
如何检索值?
要获取字段的值,您可以在循环中使用 Console.WriteLine(row.SomeField);
来接收行的 SomeField 的值。
备注
请记住,Console.WriteLine(row);
将输出您公司的类型 class,输出将是每一行的 class 名称。
您正在打印查询本身,因为 companies
包含查询。
你想要做的是,运行 查询(foreach 会做),并遍历结果集(你已经在做),对于结果集中的每一行,打印你想要的细节,比如
foreach (var row in companies) //row is each row in result set of query companies
{
Console.WriteLine(row.SomeProperty); //row, not companies
Console.WriteLine(row.SomeOtherProperty);
Console.ReadLine();
}
我有这个简单的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Entity;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
NuLabsEntities db = new NuLabsEntities();
IEnumerable<company> companies = from cmp in db.company select cmp;
foreach (var row in companies)
{
Console.WriteLine(companies);
Console.ReadLine();
}
}
}
}
我知道这是一个基本问题:我正在学习 c#
但我不明白为什么,在使用 ado.net 创建一个 edmx 文件并尝试 运行 这个简单的代码后,它 returns 我得到以下查询而不是 a 的结果公司的行列表 table:
SELECT
[Extent1].[companyId] AS [companyId],
[Extent1].[labirintoCodiceCliente] AS [labirintoCodiceCliente],
[Extent1].[labirintoCodiceAteco2007] AS [labirintoCodiceAteco2007],
[Extent1].[name] AS [name],
[Extent1].[doc] AS [doc],
[Extent1].[notes] AS [notes],
[Extent1].[vatNumber] AS [vatNumber],
[Extent1].[taxCode] AS [taxCode],
[Extent1].[LabirintoFornitoreId] AS [LabirintoFornitoreId],
[Extent1].[LabirintoCommercialistaId] AS [LabirintoCommercialistaId],
[Extent1].[LabirintoConsulenteDelLavoroId] AS [LabirintoConsulenteDelLavoroId]
FROM [dbo].[company] AS [Extent1]
Console.WriteLine(companies);
应该是Console.WriteLine(row.blah);
您需要调用
.ToList()
然后循环遍历集合。当您调用ToList()
. 时会评估查询
使用您编码的 foreach
,您将每个 company
放入行中。您可以从 row
.
company
的属性
让我们假设您公司的结构是这样的
public class company
{
public int companyId {get;set;}
public string companyName {get;set;}
}
你的代码应该是
foreach (var row in companies.ToList())
{
Console.WriteLine("CompanyId:"+row.CompanyId.ToString());
Console.WriteLine("CompanyName:"+row.CompanyName);
Console.ReadLine();
}
我认为你应该传递行对象
Console.WriteLine(row);
为什么?
这是因为公司类型是 System.Data.Entity.Infrastructure.DbQuery<Company>
并且它的 ToString()
方法是 returns 查询。
当你使用Console.WriteLine(somthings)
时,somethings的ToString
方法将被用于输出数据,因此你将收到ToString
结果,即Query Text。
如何检索值?
要获取字段的值,您可以在循环中使用 Console.WriteLine(row.SomeField);
来接收行的 SomeField 的值。
备注
请记住,Console.WriteLine(row);
将输出您公司的类型 class,输出将是每一行的 class 名称。
您正在打印查询本身,因为 companies
包含查询。
你想要做的是,运行 查询(foreach 会做),并遍历结果集(你已经在做),对于结果集中的每一行,打印你想要的细节,比如
foreach (var row in companies) //row is each row in result set of query companies
{
Console.WriteLine(row.SomeProperty); //row, not companies
Console.WriteLine(row.SomeOtherProperty);
Console.ReadLine();
}