c# - 如何为此进行 LINQ 查询?

标签 c# linq

好吧,卡了一天了,求高人帮帮忙!

我想选择特定客户可能拥有的每件商品的数量,无论他们是否订购了该商品。因此,如果他们没有订购它(即它不在 Orders 表中),则返回 0 的数量值。

订单 CustomerId ItemId 数量

项目 元素编号 元素名称

客户 客户编号 客户姓名

我猜这会涉及某种子查询,但真的不确定。

from c in customers
join o in Orders
on c.CustomerId == o.CustomerId
select c.CustomerName, o.Quantity
where c.CustomerId == customerId

最佳答案

这引起了我的兴趣,所以我编写了一个控制台应用程序来完成它。

Andomar 在我回答 (+1) 之前几分钟回答了我。 这是我的独立解决方案,但非常接近他的解决方案,而且并不简单......

class Program {
    private class CustomerDto {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
    private class ItemDto {
        public int ItemId { get; set; }
        public string ItemName { get; set; }
    }
    private class OrderDto {
        public int Id { get; set; }
        public int ItemId { get; set; }
        public int CustomerId { get; set; }
        public int Quantity { get; set; }
    }
    private class CustomerOrderDto {
        public int CustomerId { get; set; }
        public int ItemId { get; set; }
        public int TotalQuantity { get; set; }
    }

    static void Main(string[] args) {
        List<CustomerDto> Customers = new List<CustomerDto>() { 
            new CustomerDto() { CustomerId = 1, CustomerName = "one"},
            new CustomerDto() { CustomerId = 2, CustomerName = "two"},
            new CustomerDto() { CustomerId = 3, CustomerName = "three"}
        };
        List<ItemDto> Items = new List<ItemDto>() { 
            new ItemDto() { ItemId = 1, ItemName = "item one"},
            new ItemDto() { ItemId = 2, ItemName = "item two"},
            new ItemDto() { ItemId = 3, ItemName = "item three"}
        };
        // customer1 has 2 orders for item 1, 0 for item 2 or 3
        // customer2 has 1 order for item 2, 0 for item 1 or 3
        // customer3 has 1 order for item 2, 1 order for item 3 and 0 for item 1
        List<OrderDto> Orders = new List<OrderDto>() { 
            new OrderDto() { Id = 1, CustomerId = 1, ItemId = 1, Quantity = 3 },
            new OrderDto() { Id = 1, CustomerId = 1, ItemId = 1, Quantity = 5 },
            new OrderDto() { Id = 1, CustomerId = 3, ItemId = 2, Quantity = 5 },
            new OrderDto() { Id = 1, CustomerId = 3, ItemId = 3, Quantity = 5 },
            new OrderDto() { Id = 1, CustomerId = 2, ItemId = 2, Quantity = 5 }
        };
        List<CustomerOrderDto> results = (from c in Customers
                                          from i in Items
                                          join o in Orders on
                                            new { c.CustomerId, i.ItemId } equals
                                            new { o.CustomerId, o.ItemId } into oj
                                          from o in oj.DefaultIfEmpty()
                                          let x = o ?? new OrderDto() { CustomerId = c.CustomerId, ItemId = i.ItemId, Quantity = 0 }
                                          group x by new { x.CustomerId, x.ItemId } into g
                                          select new CustomerOrderDto() {
                                              CustomerId = g.Key.CustomerId,
                                              ItemId = g.Key.ItemId,
                                              TotalQuantity = g.Select(x => x.Quantity).Sum()
                                          }
                                          ).ToList();
        foreach (var result in results) {
            Console.WriteLine("Customer {0} purchased {1} units of item {2}",
                result.CustomerId, result.TotalQuantity, result.ItemId);
        }
        Console.ReadKey(true);
    }
}

关于c# - 如何为此进行 LINQ 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180543/

相关文章:

c# - 在 SignalR V 2.0 中支持驼峰式属性名称

c# - List.Any 得到匹配的字符串

c# - 亚音速 : Self Join , 表别名

c# - 犀牛模拟 : How to verify that a constructor was called

c# - 为什么从 C# 为 IronPython 脚本调用 CompiledCode.Execute 的行为不符合预期

C# - 将类的新实例添加到数组

c# - RadGrid - 每列批量编辑多个编辑模板

c# - 延迟执行 - 字典输出

c# - 如何创建一个空的 SelectList

c# - 我应该如何获得 IEnumerable 的长度?