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

Returning Anonymous type in LINQ

1 Answer 108 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Zaly
Top achievements
Rank 1
Zaly asked on 22 Oct 2011, 03:46 PM
Hi,

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();
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Thomas
Telerik team
answered on 24 Oct 2011, 09:32 AM
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,
Thomas
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

Tags
LINQ (LINQ specific questions)
Asked by
Zaly
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Share this question
or