IEnumerable和console.writeline

阿莱西奥·巴辛(Alessio Bacin)

我有这个简单的代码:

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文件并尝试运行此简单代码后,它向我返回以下查询,而不是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]
萨特什·帕古鲁(Sateesh Pagolu)
  1. Console.WriteLine(companies); 应该 Console.WriteLine(row.blah);

  2. 您需要调用.ToList()然后遍历集合。调用时评估查询ToList()

有了foreach这些代码,您就可以将每一company行都放入行中。您可以访问的特性companyrow

假设您的comany的结构是这样

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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章