Returning Anonymous type in LINQ

Thread is closed for posting
2 posts, 1 answers
  1. Zaly
    Zaly avatar
    7 posts
    Member since:
    Sep 2011

    Posted 22 Oct 2011 Link to this post


    I've just started using your OpenAccess tool which I've just purchased. Following your example in the documentation, I successfully completed defining my basic project with your OpenAccess tool error-free. Kudos to Telerik ORM team.

    In the example that I followed, I created a simple LINQ query to return to the caller function all fields in the Products entity which is shown in the commented section in the code below. Next, I wanted to create a slightly more complex LINQ query that only selects certain fields (and, in my real project, I would need to join several tables/entities). That's when I faced problems. I search the internet and the discussions mention about problems with returning Anonymous type. This is a real need on my side (i.e. to be able to join tables and return selected fields only). Can you please help me?

    public IEnumerable<Product> LoadProductsByAgency(int AgencyId)
                var p = (from c in this.dbContext.Products
                             where c.Product_Agency == 1
                             select new {ProductName = c.ProductName,
                                        ProductType = c.Product_ProductType,
                                        Company = c.Product_Company}).ToList();
                return p;
                //return this.dbContext.Products.Where((Products) => Products.Product_Agency == 1).ToList();
  2. Answer
    Thomas avatar
    590 posts

    Posted 24 Oct 2011 Link to this post

    Hi Zaly,

    using anonymous types to carry the selected values is not a problem. You do it in the way that you already wrote. I (personally) prefer the style of declaring the logical query and have a separate step that executes the query with the .ToList() method, but this is just personal taste.
    What are the difficulties that you faced?

    Kind regards,
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

Back to Top