This is a migrated thread and some comments may be shown as answers.

LINQ (or Business Object Datasource)

8 Answers 1417 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Yuriy Rogach
Top achievements
Rank 1
Yuriy Rogach asked on 02 Oct 2008, 09:23 PM
Hi All,

I'm interesting is it possible to use Business Object Datasource for reporting ?

I mean how to display complex values, let me explain with example:

I have classes:
public class SubClass  
{  
  public string Name {get;set;}  
}  
 
public class MyClass  
{  
  public string Name {get;set;}  
  public SubClass subClass {get;set;}  

And also have a
List<MyClass> list = MyBusinessLogic.GetList(); 

I know how to set report datasource to list, but how to display MyClass.subClass.Name ?
 =Fields.subClass.Name don't work.

Thanks.

8 Answers, 1 is accepted

Sort by
0
Scott R
Top achievements
Rank 1
answered on 03 Oct 2008, 04:23 AM
We use Linq to generate an object collection used for reporting. We "flatten" the business objects using a Linq query like this:

MyDataContext dc = new MyDataContext(); 
 
var q = from c in dc.MyClass 
        select new 
        { 
            c.Name, 
            SubName = c.subClass.Name 
        } 
 
this.DataSource = q.ToList();
0
Yuriy Rogach
Top achievements
Rank 1
answered on 03 Oct 2008, 06:34 AM
Hi Scott,

Thanks for reply. I'm also know this way :-)

Just was interesting, because RadGridView works fine with field value like "myClass.subClass.Name". Anyway thank you.
0
Accepted
Rossen Hristov
Telerik team
answered on 03 Oct 2008, 09:26 AM
Hi Yuriy Rogach,

Telerik Reporting does support deep object graphs, i.e. business objects with complex properties. I have prepared a small demo report that demonstrates that. The report is bound to an imaginary bookstore that returns a list of books. Each book has an year (primitive type) and an Author (complex type). Each author has a name (primitive type).

On the report both the year (=Fields.Year) and the author name (=Fields.Author.Name) are displayed. Here is a code snippet from the sample:

namespace BusinessObjectsReport 
    using System; 
    using System.Collections.Generic; 
    using Telerik.Reporting; 
 
    public partial class BusinessObjectsReport : Report 
    { 
        public BusinessObjectsReport() 
        { 
            InitializeComponent(); 
            BookStore bookStore = new BookStore(20); 
            this.DataSource = bookStore.Books; 
        } 
    } 
     
    class BookStore 
    { 
        List<Book> books = new List<Book>(); 
 
        public BookStore(int bookCount) 
        { 
            for (int i = 0; i < bookCount; i++) 
            { 
                this.books.Add(new Book()); 
            } 
        } 
 
        public List<Book> Books 
        { 
            get { return books; } 
        } 
    } 
 
    class Book 
    { 
        static Random random = new Random(); 
        int year = random.Next(1900, 2008); 
        Author author = new Author(); 
 
        public int Year 
        { 
            get { return this.year; } 
        } 
 
        public Author Author 
        { 
            get { return this.author; } 
        } 
    } 
 
    class Author 
    { 
        static Random random = new Random(); 
        static string[] names = new string[]{"John" 
            , "Paul" 
            , "George" 
            , "Elizabeth" 
            , "Andrew"}; 
        static string[] families = new string[]{"Smith" 
            , "Jones" 
            , "Williams" 
            , "Brown" 
            , "Taylor"}; 
 
        string name; 
 
        public Author() 
        { 
            this.name = string.Format("{0} {1}" 
                , names[random.Next(0, 5)] 
                , families[random.Next(0, 5)]); 
        } 
 
        public string Name 
        { 
            get { return this.name; } 
        } 
    } 

And here is how the report looks at design-time:



Here is the rendered report:



I have attached the sample report. Please examine it carefully to see its implementation. If you are not using the latest version of Telerik Reporting, please upgrade to it since this feature was implemented recently. If nothing helps, please open a separate support ticket and send us your report. We will examine it to see what is going wrong and why.

I hope that the sample will get you stared. If you have any other questions or problems, please do not hesitate to contact us.

Kind regards,
Ross
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yuriy Rogach
Top achievements
Rank 1
answered on 05 Oct 2008, 02:39 PM
Hi Ross,

Thanks for example, my problem was in using not latest version of Telerik Reporting. Now everything is OK.
0
p3mek
Top achievements
Rank 1
answered on 16 Dec 2008, 01:38 AM
Yes, but what if you also wanted to display some information about the Book Store?

Let's say we added a Property to the BookStore called [Name] and instead of binding the report to BookStore.Books, we bind it to BookStore and use the [=Fields.Name] to display the Name of the BookStore. How would we then format the Details Section to display the List of books? Using [=Fields.Books.Year] and [=Fields.Books.Author.Name] doesn't work.
Am I missing something simple here?
0
Steve
Telerik team
answered on 16 Dec 2008, 03:50 PM
Hi p3mek,

You cannot do this the way you have described. The StoreName is one level up and to "preserve" the hierarchy of the data, you would have to create a master-detail report, and bind the master report to the Storename and the Subreport to the Author.Name.

Regards,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rizwan
Top achievements
Rank 1
answered on 04 Nov 2010, 10:53 PM

Hi;

I am facing problem with crosstab bindingd source. its not binding and there is no data
in my report above the cross tab there is a table I have written the same code on detailsection_itembinding. 
Can you please guide me whats wrong I am doing there.
Added to it is there any way to set the report crosstab/table/chart data source from the report viewer page. Its urgent as I need to deploy the next release on sunday. 
        private void detail_ItemDataBinding(object sender, EventArgs e)
        {
            var detail = sender as Telerik.Reporting.Processing.DetailSection;
            var processingTable1 = detail.ChildElements.Find("table1", true)[0] as Telerik.Reporting.Processing.Table;
            var c = Query.All<LossEvent>();
            processingTable1.DataSource = c;

        }
// Cross tab needdatasource
private void crosstab1_NeedDataSource(object sender, EventArgs e)
{
    Telerik.Reporting.Processing.Table crosstab1 = (Telerik.Reporting.Processing.Table)sender;
 
    crosstab1.DataSource = from o in Query.All<LE>().Where(o => o.ObjectStatus != "Deleted")
                           orderby o.Name
                           select o;
}

0
japss
Top achievements
Rank 1
answered on 22 Apr 2011, 07:23 PM
Why am I not able to access the public variables from report?

In the above BookStore example, if you peek into Books objects 
this.DataSource = bookStore.Books;
you would notice that it has both public variable and get property (year and Year).  I could access both of them in the report --> =Fields.year or =Fields.Year - looks good.

int year = random.Next(1900, 2008); 
        Author author = new Author(); 
 
        public int Year 
        { 
            get { return this.year; } 
        } 
Now, if you remove the "get property" (Year) leaving the public variable to the class, you get an error - The expression contains object "year" does not exist.

That means, I should always have get property in order to use it from the report?

Question 2: Do you have an example on how to implement Steve's suggestion for user, p3mek? The link for master-detail also broken.
Tags
General Discussions
Asked by
Yuriy Rogach
Top achievements
Rank 1
Answers by
Scott R
Top achievements
Rank 1
Yuriy Rogach
Top achievements
Rank 1
Rossen Hristov
Telerik team
p3mek
Top achievements
Rank 1
Steve
Telerik team
Rizwan
Top achievements
Rank 1
japss
Top achievements
Rank 1
Share this question
or