将匿名类型转换为已知类型

时间:2010-08-04 18:12:56

标签: c# linq

我有一个包含以下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

3 个答案:

答案 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)

是的,你不能这样投。您可以在MesReport类中添加一个构造函数,该类具有与您列出的相同顺序的参数:

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(),这样你就不必在返回它时抛出它。