Simple linq2sql query to display in crystal report

2019-08-26 12:17发布

问题:

I am new to crystal reports and would like to to display sql data on a crystal report using linq2sql. So far i am just trying to display one field (tripNo) with no luck. My error is the data source object is invalid. Here is my code.

private void runstuff()
    {
        using (DataClasses1DataContext db = new DataClasses1DataContext())
        {
            var test = (from s in db.trips
                        select s.tripNo).First();

            CrystalReport1 cr1 = new CrystalReport1();
            cr1.SetDataSource(test);
            crystalReportViewer1.ReportSource = cr1;
        }
    }

回答1:

It's been quite a while since I've used Crystal. However, the problem seems to be the type of 'test'. Crystal probably doesn't know what to do with this. You might want to try this. Note, I've removed the First() method so that a list of rows are returned and then I convert this to a List of entities. Crystal should be able to handle this.

var test = (from s in db.trips
            select s.tripNo).ToList();

            CrystalReport1 cr1 = new CrystalReport1();
            cr1.SetDataSource(test);

Hope this helps.



回答2:

The app.config file needed this line

<startup useLegacyV2RuntimeActivationPolicy="true">