【发布时间】: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