IEnumerable和console.writeline

时间:2015-09-15 13:49:57

标签: c# linq console ienumerable

我有这个简单的代码:

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文件并尝试运行这个简单的代码后,它会返回以下查询而不是公司表的行列表的结果:

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]

4 个答案:

答案 0 :(得分:2)

我认为你应该传递行对象

Console.WriteLine(row);

答案 1 :(得分:2)

<强>为什么吗

因为公司类型为System.Data.Entity.Infrastructure.DbQuery<Company>,而ToString()方法会返回查询。

当您使用Console.WriteLine(somthings)时,某些事件的ToString方法将用于输出数据,因此您将收到ToString结果,即查询文字。

我如何撤回价值?

要获取字段的值,您可以在循环中使用Console.WriteLine(row.SomeField);来接收SomeField行的值。

注意

请记住,Console.WriteLine(row);将输出您公司类的类型,输出将是每行的类名。

答案 2 :(得分:0)

  1. // in config I have set the recommended value: config.startupFocus = true; // load the editor var instance = CKEDITOR.inline(element.id, { // ... load config }); // this works to prevent hiding of the editor, I don't know yet if this breaks anything, // but seems to be working fine instance.on('blur',function(){ return false; }); // and because the startupFocus property triggers focus on each element that has the editor // I scroll the document to the top on the event when editor is ready instance.on("instanceReady", function(){ document.body.scrollTop = document.documentElement.scrollTop = 0; }); 应为Console.WriteLine(companies);

  2. 您需要调用Console.WriteLine(row.blah);然后循环访问该集合。当您致电.ToList()时,会评估查询。

  3. 对于您已编码的ToList(),您会将每个foreach放入行中。您可以从company访问company的媒体资源。

    让我们假设您的公司结构是

    row

    您的代码应为

    public class company
    {
       public int companyId {get;set;}
       public string companyName {get;set;}
    }
    

答案 3 :(得分:0)

您正在打印查询本身,因为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();
 }