Crystal Reports and LINQ

David Vidmar picture David Vidmar · Sep 19, 2008 · Viewed 12.1k times · Source

Has anyone figured out how to use Crystal Reports with Linq to SQL?

Answer

Mohammad Sepahvand picture Mohammad Sepahvand · Oct 7, 2011

You can convert your LINQ result set to a List, you need not strictly use a DataSet as the reports SetDataSource, you can supply a Crystal Reports data with an IEnumerable. Since List inherits from IEnumerable you can set your reports' Data Source to a List, you just have to call the .ToList() method on your LINQ result set. Basically:

        CrystalReport1 cr1 = new CrystalReport1();

        var results = (from obj in context.tSamples
                      where obj.ID == 112
                      select new { obj.Name, obj.Model, obj.Producer }).ToList();

        cr1.SetDataSource(results);
        crystalReportsViewer1.ReportSource = cr1;