0

我正在尝试创建一个用于从单个数组中选择多个项目的包装器。我在下面的代码末尾得到结果。不知道我做错了什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tester.cs
{
class Program
{
    static void Main(string[] args)
    {
        var customers = new[] 
        {
            new { CustomerID = 1, FirstName = "Orlando", LastName = "Gee",
            CompanyName = "A Bike Store" },
            new { CustomerID = 2, FirstName = "Keith", LastName = "Harris",
            CompanyName = "Bike World" },
            new { CustomerID = 3, FirstName = "Donna", LastName = "Carreras",
            CompanyName = "A Bike Store" },
            new { CustomerID = 4, FirstName = "Janet", LastName = "Gates",
            CompanyName = "Fitness Hotel" },
            new { CustomerID = 5, FirstName = "Lucy", LastName = "Harrington",
            CompanyName = "Grand Industries" },
            new { CustomerID = 6, FirstName = "David", LastName = "Liu",
            CompanyName = "Bike World" },
            new { CustomerID = 7, FirstName = "Donald", LastName = "Blanton",
            CompanyName = "Grand Industries" },
            new { CustomerID = 8, FirstName = "Jackie", LastName = "Blackwell",
            CompanyName = "Fitness Hotel" },
            new { CustomerID = 9, FirstName = "Elsa", LastName = "Leavitt",
            CompanyName = "Grand Industries" },
            new { CustomerID = 10, FirstName = "Eric", LastName = "Lang",
            CompanyName = "Distant Inn" }
                        };

        var addresses = new[] { 
            new { CompanyName = "A Bike Store", City = "New York", Country = "United States"},
            new { CompanyName = "Bike World", City = "Chicago", Country = "United States"},
            new { CompanyName = "Fitness Hotel", City = "Ottawa", Country = "Canada"},
            new { CompanyName = "Grand Industries", City = "London", Country = "United Kingdom"},
            new { CompanyName = "Distant Inn", City = "Tetbury", Country = "United Kingdom"}
        };

        IEnumerable<Names> customerfullName = customers.Select(data => new Names { 
        FirstName = data.FirstName,
        LastName = data.LastName});

        foreach (Names entry in customerfullName)
        {
            Console.WriteLine(entry);
        }  
    }
} 
    class Names
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Tester.cs.Names 是我运行程序时重复的内容。

4

3 回答 3

1

Console.WriteLine使用类的ToString方法object。默认情况下,显示类的名称。

此方法被派生的类覆盖,object以显示他们想要的任何内容。你没有覆盖它,所以你得到了默认值。

您可以在没有 LINQ 的情况下重现您的问题,如下所示:

class Names
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

var name = new Names {FirstName = "John", LastName = "Saunders"};
Console.WriteLine(name);  // Will display "Tester.cs.Names"
于 2013-09-24T20:49:52.497 回答
0

您的Names类没有覆盖该ToString方法,因此它使用默认实现object并打印出它的类型名称。您要么需要覆盖ToStringNames打印出底层字符串,要么需要打印出foreach循环中的各个字符串属性。

于 2013-09-24T20:50:26.390 回答
0

默认将使用 ToString,使用:

class Names
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}

也可以为全名创建一个额外的属性

class Names
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    }
}

用法:

foreach (Names entry in customerfullName)
{
    Console.WriteLine(entry.FullName);
}      
于 2013-09-24T20:51:10.267 回答