我在observablecollection中有订单号,物品和数量的订单。我想将字符串作为第一个订单编号,并在其下列出项目和计数数量(没有零数量)。像:
order1
apple 2
lemon 1
banana 1
order2
apple 3
香蕉3
我可以用我的代码完成它,但它不能成为正确的方法。所以我在问它应该怎么做或者做什么是好方法。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace OrdersArrange
{
public static class EnumerableExtensions{
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (var e in enumerable)
{
action(e);
}
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Order> _order;
public MainWindow()
{
InitializeComponent();
CreateOrders();
ListOfOrdersToString();
}
private void CreateOrders()
{
_order = new ObservableCollection<Order>();
var order = new Order()
{
OrderNumber = "1111",
OrderItem = "Apple",
ItemCount = 2
};
_order.Add(order);
order = new Order()
{
OrderNumber = "1111",
OrderItem = "Banana",
ItemCount = 0
};
_order.Add(order);
order = new Order()
{
OrderNumber = "2222",
OrderItem = "Banana",
ItemCount = 5
};
_order.Add(order);
order = new Order()
{
OrderNumber = "2222",
OrderItem = "Banana",
ItemCount = 1
};
_order.Add(order);
order = new Order()
{
OrderNumber = "1111",
OrderItem = "Banana",
ItemCount = 3
};
_order.Add(order);
order = new Order()
{
OrderNumber = "3333",
OrderItem = "Banana",
ItemCount = 1
};
_order.Add(order);
order = new Order()
{
OrderNumber = "3333",
OrderItem = "Lemon",
ItemCount = 2
};
_order.Add(order);
order = new Order()
{
OrderNumber = "1111",
OrderItem = "Lemon",
ItemCount = 0
};
_order.Add(order);
order = new Order()
{
OrderNumber = "2222",
OrderItem = "Banana",
ItemCount = 2
};
_order.Add(order);
order = new Order()
{
OrderNumber = "1111",
OrderItem = "Lemon",
ItemCount = 1
};
_order.Add(order);
order = new Order()
{
OrderNumber = "1111",
OrderItem = "Apple",
ItemCount = 0
};
_order.Add(order);
}
private void ListOfOrdersToString()
{
// List for order numbers.
var listOfOrderNumbers = new List<string>();
string ordersList = "This contains the orders: " + "\n";
// Loop throught order numbers.
for (int i = 0; i < _order.Count; i++)
{
// Check order number.
if (!listOfOrderNumbers.Contains(_order.ElementAt(i).OrderNumber))
{
// Add order number to list.
listOfOrderNumbers.Add(_order.ElementAt(i).OrderNumber);
// Add order number line to string.
ordersList += _order.ElementAt(i).OrderNumber + "\n";
// List for item names.
var listOfItems = new List<string>();
// Loop throught items.
for (int o = 0; o < _order.Count; o++)
{
// Check item number.
if (!listOfItems.Contains(_order.ElementAt(o).OrderItem))
{
// Add item to list.
listOfItems.Add(_order.ElementAt(o).OrderItem);
// Count quantity.
int quantity = 0;
_order.Where(x => x.OrderNumber == _order.ElementAt(i).OrderNumber).Where(x => x.OrderItem == _order.ElementAt(o).OrderItem).Where(x => x.ItemCount > 0).ForEach(a => quantity = (quantity + a.ItemCount));
if (quantity > 0)
{
ordersList += _order.ElementAt(o).OrderItem + "\t" + quantity + "\n";
}
}
}
ordersList += "-----------------------" + "\n";
}
}
MessageBox.Show(ordersList);
Console.WriteLine(ordersList);
}
}
public class Order
{
public string OrderNumber { get; set; }
public string OrderItem { get; set; }
public int ItemCount { get; set; }
}
}
打印出: 这包含订单:
1111
Apple 2
香蕉3
柠檬1
2222
香蕉8
3333
香蕉1
柠檬2
答案 0 :(得分:0)
我会以这种方式制作它(我用它编译器,所以我希望没有那么多的错误):
public void ListOfOrdersToString()
{
//somehow fill _order
List<Order> _order = null;
List<CompleteOrder> completeOrders = new List<CompleteOrder>();
foreach (Order order in _order)
{
CompleteOrder completeOrder = completeOrders.Where(o => o.OrderID == Int32.Parse(order.OrderNumber)).SingleOrDefault();
if (completeOrder == null)
{
completeOrder = new CompleteOrder();
completeOrder.OrderID = Int32.Parse(order.OrderNumber);
completeOrders.Add(completeOrder);
}
var count = completeOrder.OrderItems.FirstOrDefault(x => x.Key == order.OrderItem).Value;
if (count == null)
{
completeOrder.OrderItems.Add(order.OrderItem, order.ItemCount);
}
else
{
count = count + order.ItemCount;
}
}
print(completeOrders);
}
public void print(List<CompleteOrder> completeOrderList)
{
foreach (CompleteOrder co in completeOrderList)
{
Console.WriteLine(co.OrderID);
foreach (KeyValuePair<string, int> entry in co.OrderItems)
{
Console.WriteLine(entry.Key + " " + entry.Value + "\n");
}
}
}
}
public class CompleteOrder
{
public int OrderID { get; set; }
public Dictionary<String, int> OrderItems { get; set; }
}