如何在应用程序中最好地处理数据

时间:2016-06-13 19:25:41

标签: c# wpf linq ienumerable

使用Payroll的C#应用​​程序。作为一个警告,我对这一切都很陌生,只是被吸进去享受它,但是在很短的时间内吸收很多东西。我目前正在学习LINQ和课程。我从我们的账户SQL服务器中获取发票行,并根据销售情况支付人员,并根据数据确定一些情况。请参阅以下示例:

Ord_Inv_No      Delivery_Date       Trmnl_Key   TrmnlGrp_Key
INV-00059754    2016-05-02 07:00:00 23          3
INV-00059839    2016-05-02 16:01:00 56          3

这是数据的一小部分,但它给出了我想要分析的要点。我有一个课程如下所示:

class SalesItem
{

    public string DriverName { get; set; }
    public DriverData DriverData { get; set; }
    public int TerminalKey { get; set; }
    public int TerminalGroupKey { get; set; }
    public string DeliveryDate { get; set; }
    public string CustomerLocation { get; set; }
    public int? CustomerLocationKey { get; set; }
    public bool? IsCredited { get; set; }
    public string InvoiceNumber { get; set; }
    public string TermianlGroupDesc { get; set; }

    public SalesItem(string driverName, int terminalKey, int terminalGroupKey, string deliveryDate, 
        string customerLocation, int? customerLocationKey, bool? isCredited, string invoiceNumber, string terminalGroupDesc)
    {
        this.DriverName = driverName;
        this.TerminalKey = terminalKey;
        this.TerminalGroupKey = terminalGroupKey;
        this.DeliveryDate = deliveryDate;
        this.CustomerLocation = customerLocation;
        this.CustomerLocationKey = customerLocationKey;
        this.IsCredited = isCredited;
        this.InvoiceNumber = invoiceNumber;
        this.TermianlGroupDesc = terminalGroupDesc;
    }

我正在尝试实现一种方法1.将每个SalesItem(每一行)存储为一个缺少更好的单词的集合。我想迭代SalesItem的集合并找出一些内容,例如当一个发票行显示两次时使用不同的Trmnl_Key和两个不同的Ord_Inv_No具有完全相同的日期时作为彼此并返回两者的最高工资率。我为此编写了一些逻辑,它正在操纵我通过SQLConnection / Command / Reader / Etc抓取的DataTable,但是当处理一周的数据并且我无法获得时,所有这些都非常缓慢和繁琐比较工作可靠。更进一步,这些行将在WPF中的DataGrid中进行标记,以便在会计软件出现问题时验证其工资。

现在,我正在尝试保存集合,如下所示(这只是当前的迭代,我有几天值得尝试失败。)

 private void GetInformation(object sender, SelectionChangedEventArgs e)
    {
        ComboBox currentItem = sender as ComboBox;
        int driverKey = Convert.ToInt32(currentItem.SelectedValue);
        DriverData driver = new DriverData();
        driver.SetDriverData(driverKey);
        int years = driver.ServiceYears;
        List<SalesItem> items =  (from o in transportationDb._Payroll_Orders
                                 where o.Ord_Driver_Key == driverKey &&
                                 o.Delivery_Date >= new DateTime(2016, 5, 2) && o.Delivery_Date <= new DateTime(2016, 5, 8)
                                 select new SalesItem(
                                     o.Driver_Name,
                                     o.Trmnl_Key,
                                     o.TrmnlGrp_Key,
                                     o.Delivery_Date.ToString(),
                                     o.CustLoc_Description,
                                     o.Ord_CustLoc_Key,
                                     o.Credits, o.Ord_Inv_No, o.TrmnlGrp_Description )).ToList();


        dataGrid.ItemsSource = items;

    }

非常感谢任何智慧或方向

2 个答案:

答案 0 :(得分:0)

当然,这与你所拥有的不同,但试试这个并看看它是否适合你:

private void GetInformation(object sender, SelectionChangedEventArgs e)
{
    ComboBox currentItem = sender as ComboBox;
    int driverKey = Convert.ToInt32(currentItem.SelectedValue);
    DriverData driver = new DriverData();  //What's this for?
    driver.SetDriverData(driverKey); //What's this for?
    int years = driver.ServiceYears; //What's this for?
    var startDate=new DateTime(2016, 5, 2);
    var endDate=new DateTime(2016, 5, 8);
    var items = transportationDb._Payroll_Orders
      .Where(o=>o.Ord_Driver_Key == driverKey)
      .Where(o=>o.Delivery_Date >= startDate && o.Delivery_Date <= endDate)
      .Select(o=>new SalesItem {
        DriverName=o.Driver_Name,
        ...
      });
    dataGrid.ItemsSource = items;
}

如果您的Orders表在Ord_Driver_key和Delivery_Date上有索引,则此查询应该非常快。

答案 1 :(得分:0)

我希望这在某种程度上有所帮助。我不太确定你想要实现的目标。保存功能可能类似于以下内容,但我不确切知道您正在使用哪种连接。请注意,我正在检查以确保记录不存在。

 private void Save(SalesItem item)
    {
        if(!transportationDb._Payroll_Orders.Any(p => p.Ord_Inv_No == item.InvoiceNumber))
        {
            transportationDb._Payroll_Orders.Add(item.PayrollOrder);
            transportationDb.SaveChanges();
        }
        else
        {
            // Already exists
        }
    }

根据您的喜好更新您的课程,但我会从对您的对象看起来像的有限知识的方式这样做。

class SalesItem
{

    public SalesItem(_Payroll_Orders po == null, DriverData dd == null)
    {
        this._Payroll_Order = po != null ? po : new _Payroll_Orders();
        this.DriverData = dd != null ? dd : new DriverData();
    }

    private _Payroll_Orders _Payroll_Order { get; set; }

    public _Payroll_Orders PayrollOrder
    {
        get { return _Payroll_Order; }
        set { _Payroll_Order = value; }
    }

    public string DriverName 
    {
        get { return _Payroll_Order.Driver_Name; }
        set { _Payroll_Order.Driver_Name= value; }
    }
    public DriverData DriverData { get; set; }
    public int TerminalKey 
    {
        get { return _Payroll_Order.Trmnl_Key; }
        set { _Payroll_Order.Trmnl_Key= value; }
    }
    public int TerminalGroupKey 
    {
        get { return _Payroll_Order.TrmnlGrp_Key; }
        set { _Payroll_Order.TrmnlGrp_Key= value; }
    }
    public DateTime DeliveryDate 
    {
        get { return _Payroll_Order.Delivery_Date; }
        set { _Payroll_Order.Delivery_Date= value; }
    }
    public string CustomerLocation 
    {
        get { return _Payroll_Order.CustLoc_Description; }
        set { _Payroll_Order.CustLoc_Description = value; }
    }
    public int? CustomerLocationKey 
    {
        get { return _Payroll_Order.Ord_CustLoc_Key; }
        set { _Payroll_Order.Ord_CustLoc_Key = value; }
    }
    public bool? IsCredited 
    {
        get { return _Payroll_Order.Credits; }
        set { _Payroll_Order.Credits = value; }
    }
    public string InvoiceNumber 
    {
        get { return _Payroll_Order.Ord_Inv_No; }
        set { _Payroll_Order.Ord_Inv_No = value; }
    }
    public string TermianlGroupDesc 
    {
        get { return _Payroll_Order.TrmnlGrp_Description; }
        set { _Payroll_Order.TrmnlGrp_Description = value; }
    }
}

然后你可以在这里更改你的功能,所以你只需要选择'new SalesItem(o,driver)':

 private void GetInformation(object sender, SelectionChangedEventArgs e)
    {
        ComboBox currentItem = sender as ComboBox;
        int driverKey = Convert.ToInt32(currentItem.SelectedValue);
        DriverData driver = new DriverData();
        driver.SetDriverData(driverKey);
        int years = driver.ServiceYears;
        List<SalesItem> items =  (from o in transportationDb._Payroll_Orders
                                 where o.Ord_Driver_Key == driverKey &&
                                 o.Delivery_Date >= new DateTime(2016, 5, 2) &&
                                 o.Delivery_Date <= new DateTime(2016, 5, 8)
                                 select new SalesItem(o, driver)).ToList();

        dataGrid.ItemsSource = items;

    }