[英]IEnumerable and console.writeline
I have this simple code: 我有这个简单的代码:
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();
}
}
}
}
I know it's a basic question: I'm learning c# 我知道这是一个基本问题:我正在学习C#
But i don't understand why, after creating with ado.net an edmx file and try running this simple code it returns me the following query instead of the result of a list of rows of the company table: 但是我不明白为什么,在使用ado.net创建一个edmx文件并尝试运行此简单代码后,它向我返回以下查询,而不是company表的行列表的结果:
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(row);
Why? 为什么?
Its because type of companies is System.Data.Entity.Infrastructure.DbQuery<Company>
and the ToString()
method of it returns the Query. 这是因为公司类型为System.Data.Entity.Infrastructure.DbQuery<Company>
并且其ToString()
方法返回查询。
When you use Console.WriteLine(somthings)
the ToString
method of somethings will be used to output data, so you will receive the ToString
result that is Query Text. 当您使用Console.WriteLine(somthings)
,某物的ToString
方法将用于输出数据,因此您将收到作为查询文本的ToString
结果。
How can I retrevie values? 我该如何撤消价值观?
To get value of a field you can use Console.WriteLine(row.SomeField);
要获取字段的值,可以使用Console.WriteLine(row.SomeField);
in the loop to receive value of SomeField of row. 在循环中接收行的SomeField的值。
Note 注意
Remember that Console.WriteLine(row);
记住Console.WriteLine(row);
will output type of you company class and the output will be a class name for each row. 将输出您公司类别的类型,并且输出将是每一行的类别名称。
Console.WriteLine(companies);
should be Console.WriteLine(row.blah);
应该是Console.WriteLine(row.blah);
You need to call .ToList()
and then loop through the collection. 您需要调用.ToList()
,然后遍历整个集合。 Query is evaluated when you call ToList()
. 调用ToList()
时将评估查询。
With foreach
that you have coded, you get each company
into row. 使用已编码的foreach
,可以使每个company
排成一行。 you can access properties of company
from row
. 您可以从row
访问company
属性。
Lets assume your comany's structure is this 假设您的comany的结构是这样
public class company
{
public int companyId {get;set;}
public string companyName {get;set;}
}
Your code should be 您的代码应为
foreach (var row in companies.ToList())
{
Console.WriteLine("CompanyId:"+row.CompanyId.ToString());
Console.WriteLine("CompanyName:"+row.CompanyName);
Console.ReadLine();
}
You are printing the query itself, because companies
contains the query. 您正在打印查询本身,因为companies
包含查询。
What you want to do is, run the query (foreach will do), and iterate over the result-set (you are already doing it), and for each row in the result-set, print the details you want, like 您想要做的是,运行查询(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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.