Print Search Rate
    
   02-25-2008, 2:38 AM
How to bind a report to the data returned by the LINQ query
In order to use the result of LINQ query as the data for a report you should use Object Data Provider type of the data source.
If Object Data Provider is used then the data should be supplied at runtime, in the handler of LocateDataSource event of the ReportRuntime instance.
The handler should perform the LINQ query and set the Data property of LocateDataSourceEventArgs instance passed.
The object which the LocateDataSourceEventArgs.Data value should be set to depends on the type of the LINQ query:
 
If you use LINQ to XML, then the result returned by the LINQ is the object of type IEnumerable<XElement>. In order to use this object as the report data, you should create the collection of the business object from the collection of XElement objects and set the LocateDataSourceEventArgs.Data value to the collection of the business objects. This is possible using the anonymous types feature. Here is the sample:
        void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
        {
            XElement root = XElement.Load(@"c:\factbook.xml");
            var data = from el in root.Elements("country")
                       where (string)el.Attribute("name") == "Russia" || (string)el.Attribute("name") == "United States"
                       select new
                       {
                           Name = el.Element("Name").Value,
                           Population = el.Element("Population").Value
                       };
            args.Data = data;           
        }

If you use LINQ to SQL or LINQ to Objects, the the LocateDataSourceEventArgs.Data value should be set directly to the IEnumerable<BusinessObject> object returned by the Linq query.
        void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var data = from c in db.Customers
                    join o in db.Orders on c.CustomerID equals o.CustomerID
                    join od in db.Order_Details on o.OrderID equals od.OrderID
                    select new
                    {
                        c.CustomerID,
                        c.CompanyName,
                        o.OrderID,
                        o.OrderDate,
                        od.UnitPrice,
                        od.Quantity
                    };
            args.Data = data;
        }



Sergey Abakumoff
GrapeCity
GrapeCity » Knowledge Base » KnowledgeBase f... » How to bind a report to the data returned by the LINQ query
Privacy Policy | Copyright © 1997-2012 — GrapeCity, inc.
All trademarks mentioned are the property of their respective owners.