【问题标题】:WPF query doesn't seem appropriateWPF 查询似乎不合适
【发布时间】:2015-07-31 20:52:22
【问题描述】:

我有这个应用程序,其中报告不起作用。我是一名 PHP 开发人员,我可以理解 select * from table like 查询,但 Linq 查询对我来说是新的。

即使在 Linq 查询中也有一个连接概念,但我在这个查询中没有看到任何连接,所以我想知道这个查询有什么问题,或者在 WPF 中使用这样的查询是正常的吗?

                var query = from t in dc.TransectionsRemittenances
                            from c in t.CustomerAccounts
                            from b in t.Beneficiaries
                            from bank in b.BeneBankAccounts
                            where t.AgentId == int.Parse(agentId)
                            where t.GbBranchId == GlobalClass.GbBranchID
                            where t.Date == givenDate
                            select new { t, c, b, bank };

实际代码是这样的:

private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            string agentName = string.Empty;
            try
            {
                DateTime givenDate = DateTime.ParseExact(tbDate.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
                
                if (cmbAgents.SelectedIndex >= 0)
                {
                    agentId = ((DataRowView)cmbAgents.SelectedItem).Row.ItemArray[0].ToString();
                    agentName = ((DataRowView)cmbAgents.SelectedItem).Row.ItemArray[1].ToString();
                    //MessageBox.Show(agentId);
                    //------------------------LOAD REPORT--------------------------//

                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                    RMSDatabase2DataSet dataset = new RMSDatabase2DataSet();

                    dataset.BeginInit();

                    DataClassesDataContext dc = new DataClassesDataContext();
                    DataSet1.AgentFaxDSDataTable dt = new DataSet1.AgentFaxDSDataTable();

                    if (GlobalClass.GbBranchID != 12)
                    {
                        var query = from t in dc.TransectionsRemittenances
                                    from c in t.CustomerAccounts
                                    from b in t.Beneficiaries
                                    where t.AgentId == int.Parse(agentId)
                                    where t.GbBranchId == GlobalClass.GbBranchID
                                    where t.Date == givenDate
                                    select new { t, c, b};

                        foreach (var d in query)
                        {
                                dt.Rows.Add(d.t.ReceiverCurrency, d.t.ReceivedAmount, d.t.Date,
                                d.c.Name, d.b.Name, d.b.FName, d.b.City, d.b.Address);//, "", "", "", "");
                        }

                    }
                    else
                    {
                        var query = from t in dc.TransectionsRemittenances
                                    from c in t.CustomerAccounts
                                    from b in t.Beneficiaries
                                    from bank in b.BeneBankAccounts
                                    where t.AgentId == int.Parse(agentId)
                                    where t.GbBranchId == GlobalClass.GbBranchID
                                    where t.Date == givenDate
                                    select new { t, c, b, bank };

                        foreach (var d in query)
                        {
                            if (d.t.BeneSource == null)
                            {
                                dt.Rows.Add(d.t.ReceiverCurrency, d.t.ReceivedAmount, d.t.Date,
                                d.c.Name, d.b.Name, d.b.FName, d.b.City, d.b.Address, "", "", "", "");
                            }
                            else
                            {
                                dt.Rows.Add(d.t.ReceiverCurrency, d.t.ReceivedAmount, d.t.Date,
                                d.c.Name, d.b.Name, d.b.FName, d.b.City, d.b.Address, " (Bank: " + d.bank.BankName + ",",
                                " AccNo-" + d.bank.BankAccountNo + "-", " BCode-" + d.bank.BranchCode, ", " + d.bank.Address + ")");
                            }
                        }
                        //query.ToArray().
                    }
                    
                    reportDataSource1.Name = "DataSet1"; //Name of the report dataset in our .RDLC file
                    reportDataSource1.Value = dt;

                    
                    this._reportViewer.LocalReport.DataSources.Clear();
                    this._reportViewer.LocalReport.DataSources.Add(reportDataSource1);
                    this._reportViewer.LocalReport.ReportEmbeddedResource = "RMS.rptRemittances.rdlc";

                    ReportParameter[] param = new ReportParameter[2];
                    param[0] = new ReportParameter("para_agent", agentName.ToString());
                    param[1] = new ReportParameter("para_forDate", givenDate.ToString("d/M/yyyy"));

                    this._reportViewer.LocalReport.SetParameters(param);

                    _reportViewer.RefreshReport();
                }
            }
            catch (Exception ex)
            {
                string messageBoxText = "Enter Date Formate is incorrect, it must be 'MM/DD/YYYY'.\n\n" + ex.Message;
                string caption = "Error";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                MessageBox.Show(messageBoxText, caption, button, icon);

            }
        }

这个查询可以吗?如果可以,那么它是如何工作的?我的意思是关于连接、内连接或左连接等。加上这个查询的名称是什么,如果不是 Linq 查询,我应该在互联网上寻找什么?

因为我在我公司以前的开发人员开发的应用程序中的这个查询没有任何问题。但我也没有得到任何数据——这就是为什么我试图检查这个查询是否正常并想在 MSSQL 中运行它以查看它是否产生任何结果。但我不确定如何处理这些连接。

【问题讨论】:

  • 你熟悉 LinkPad.. 尝试使用类似的工具来生成正确的 linq 查询
  • @MethodMan 好的。我会尝试链接板

标签: c# sql-server wpf linq


【解决方案1】:

就像在 SQL Server 中一样,在 Linq 中省略连接子句会有效地创建一个 Cross Join,从而产生一个 Cartesian product。

交叉连接的需求相对很少,但是如果不知道报告显示的所有细节,就不可能说它是对还是错。

与 SQL 中一样,WHERE 子句可以有效地将交叉连接转换为内连接,这可能是预期的结果。

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多