tags:

views:

81

answers:

3

I have a method that contains the following Linq to SQL code:

    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;
    }

I'm getting an unable to cast error in trying to cast 'results' to the type shown in the return statement. Should I be able to do this? An L2SBusinessEntities.Report.MesReport entity looks as follows:

int MesReportID
int ParentID
string ReportTitle
string ReportName
string DatabaseServer
string DatabaseName
string Login
byte[] ReportFile
+4  A: 

Your method does not need casting. You can use the following code to return List of your objects:

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;
        }

update -------------

another option for your query:

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;
        }
Pavel Morshenyuk
@Pavel. Thank you but this does not work. It compiles fine, but when I run it, I get an error saying, "Explicit construction of entity type L2SBusinessEntities.Report.MesReport in query is not allowed".
Randy Minder
Try my updated example, please. It seems that L2SBusinessEntities.Report.MesReport is entity class. You can retrieve them using queries, but you can't create entities as part of a query.
Pavel Morshenyuk
I just thought, is your database.MesReport contains L2SBusinessEntities.Report.MesReport entites? in this case you can simply write: var results = (from report in database.MesReport select report).ToList();
Pavel Morshenyuk
@Pavel: `var results = database.MesReport.ToList();`, _please_
SLaks
@Pavel - Yes I could certainly do what you describe (or SLaks) but the point is that I don't want the contents of the ReportFile column included. This is a VarBinary column and I don't need or want it for this query. This is why I'm providing my own value for it.
Randy Minder
A: 

change your code to something like:

....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 
};

If you wrap all of that in parens, you can then even call .ToList() on the LINQ query itself so you don't have to cast it when you return it.

AllenG
+1  A: 

Yeah, you cannot cast like that. You could add a constructor to your MesReport class that has params in the same order you listed to do this:

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);
Steve Danner