我有一个包含以下Linq to SQL代码的方法:
public List<L2SBusinessEntities.Report.MesReport> GetListForReportTree(MESProductionDatabase database)
{
byte[] byteArray = new byte[1];
var results =
from report in database.MesReport
select new { report.MesReportID,
report.ParentID,
report.ReportTitle,
report.ReportName,
report.DatabaseServer,
report.DatabaseName,
report.Login,
ReportFile = byteArray };
return (List<L2SBusinessEntities.Report.MesReport>)results;
}
我在尝试将'results'强制转换为return语句中显示的类型时遇到错误。我应该能做到吗? L2SBusinessEntities.Report.MesReport实体如下所示:
int MesReportID
int ParentID
string ReportTitle
string ReportName
string DatabaseServer
string DatabaseName
string Login
byte[] ReportFile
答案 0 :(得分:4)
您的方法不需要施法。您可以使用以下代码返回对象列表:
public List<L2SBusinessEntities.Report.MesReport> GetListForReportTree(MESProductionDatabase database)
{
byte[] byteArray = new byte[1];
var results = (
from report in database.MesReport
select new L2SBusinessEntities.Report.MesReport {
MesReportID = report.MesReportID,
ParentID = report.ParentID,
ReportTitle = report.ReportTitle,
ReportName = report.ReportName,
DatabaseServer = report.DatabaseServer,
DatabaseName = report.DatabaseName,
Login = report.Login,
ReportFile = byteArray }).ToList();
return results;
}
更新-------------
您的查询的另一个选项:
public List<L2SBusinessEntities.Report.MesReport> GetListForReportTree(MESProductionDatabase database)
{
byte[] byteArray = new byte[1];
var results = (
from report in database.MesReport
select new {
MesReportID = report.MesReportID,
ParentID = report.ParentID,
ReportTitle = report.ReportTitle,
ReportName = report.ReportName,
DatabaseServer = report.DatabaseServer,
DatabaseName = report.DatabaseName,
Login = report.Login,
ReportFile = byteArray })
.AsEnumerable()
.Select(c => new L2SBusinessEntities.Report.MesReport{
MesReportID = c.MesReportID,
ParentID = c.ParentID,
ReportTitle = c.ReportTitle,
ReportName = c.ReportName,
DatabaseServer = c.DatabaseServer,
DatabaseName = c.DatabaseName,
Login = c.Login,
ReportFile = c.ReportFile }).ToList();
return results;
}
答案 1 :(得分:1)
var results =
from report in database.MesReport
select new L2SBusinessEntities.Report.MesReport(report.MesReportID,
report.ParentID,
report.ReportTitle,
report.ReportName,
report.DatabaseServer,
report.DatabaseName,
report.Login,
ReportFile = byteArray);
答案 2 :(得分:0)
将您的代码更改为:
....your code through "from report in database.MesReport" here
select new L2SBusinessEntities.Report.MesReport() {
report.ParentID,
report.ReportTitle,
report.ReportName,
report.DatabaseServer,
report.DatabaseName,
report.Login,
ReportFile = byteArray
};
如果你将所有这些包装在parens中,你甚至可以在LINQ查询本身上调用.ToList()
,这样你就不必在返回它时抛出它。