繁体   English   中英

LINQ -c#中的ToList()

[英]ToList() in LINQ -c#

我有一个LINQ查询,该查询根据他们的国家/地区结合了两个列表( suppliercustomers )。

创建的供应商和客户类别如下。 除此之外,我还创建了一个另外的class SupplierCustomer客户,以便将结果添加到新列表中。

public class Supplier
{
    public string SupplierName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public Order[] Orders { get; set; }
}

public class SupplierCustomer
{
    public SupplierCustomer()
    {
    }

    public string SupplierName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }

    public string Region { get; set; }
    public string PostalCode { get; set; }

    public string Phone { get; set; }
    public string Fax { get; set; }
    public Order[] Orders { get; set; }
}

//returns empty result    

List<Customer> customers = GetCustomerList();
List<Supplier> suppliers = GetSupplierList();

List<SupplierCustomer> lt =
  (from sup in suppliers
   join cust in customers on sup.Country equals cust.Country
   select new SupplierCustomer()).ToList<SupplierCustomer>();

//working query
var custSupJoin =
  from sup in suppliers
  join cust in customers on sup.Country equals cust.Country
  select new { Country = sup.Country, SupplierName = sup.SupplierName, CustomerName = cust.CompanyName };
int i = 0;
foreach (var item in custSupJoin)
{
    output = output + "\n " + i + "." + "Country=" + item.Country.ToString() + ", Supplier = ," + " Customer =" + item.SupplierName.ToString() + item.CustomerName + "\n";
    i++;
}

在此处输入图片说明 为什么第一个查询不起作用? 我需要在单个查询中将结果转换为新列表。

我简化了您的问题,并一并提供了答案

供应商类别

class Supplier
{
    public int Id { get; set; }
    public string Country { get; set; } // what you're using to join to customer
    // the rest of your properties
}

客户类别

class Customer
{
    public int Id { get; set; }
    public string Country { get; set; }  // what you're using to join to supplier
    // the rest of your properties
}

供应商客户类别

这就是您的SupplierCustomer类所需要做的,实际上,这将极大地改善代码的可维护性,因为它始终可以完美地映射到上述类型。

class SupplierCustomer
{
    public Supplier Supplier { get; set; }
    public Customer Customer { get; set; }
}

样品和解决方案

void Main()
{
    var suppliers = new List<Supplier>()
    {
        new Supplier() { Id = 1, Country = "USA" },
        new Supplier() { Id = 2, Country = "Japan" },
    };

    var customers = new List<Customer>()
    {
        new Customer() { Id = 10, Country = "USA" },
        new Customer() { Id = 11, Country = "USA" },
        new Customer() { Id = 20, Country = "Japan" },
        new Customer() { Id = 21, Country = "Japan" },
    };


    // Solution: 
    // Lambda-styled LINQ query to merge the two types

    List<SupplierCustomer> supplierCustomers = suppliers.Join(
        customers,
        supplier => supplier.Country, // primary key
        customer => customer.Country, // foreign key
        (supplier, customer) => new SupplierCustomer
        {
            Supplier = supplier,
            Customer = customer
        }).ToList();

    // Read up about Enumerations and the issues with doing a ToList() early.
    // You should avoid doing this heavy call in production and
    // try and optimize the result first!
}

替代样式查询

与上述相同,但以您要求的格式。

List<SupplierCustomer> query = (
    from customer in customers
    join supplier in suppliers on customer.Country equals supplier.Country
    select new SupplierCustomer
    {
      Supplier = supplier,
      Customer = customer
    })
    .ToList();

以上查询将产生一个列表

// USA
SupplierCustomer[0] = Supplier { Id = 1, Country = "USA" }
                      Customer { Id = 10, Country = "USA" }
SupplierCustomer[1] = Supplier { Id = 1, Country = "USA" }
                      Customer { Id = 11, Country = "USA" }
// Japan
SupplierCustomer[2] = Supplier { Id = 2, Country = "Japan" }
                      Customer { Id = 20, Country = "Japan" }
SupplierCustomer[3] = Supplier { Id = 2, Country = "Japan" }
                      Customer { Id = 21, Country = "Japan" }

注意:您可以在合并的 SupplierCustomer类型中访问Supplier类型和Customer类型,如下所示:

SupplyCustomer supplierCustomer = new SupplyCustomer();
Customer customer = supplierCustomer.Customer;
Supplier supplier = supplierCustomer.Supplier;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM