views:

26

answers:

1

I'm using Dynamic Expression API (System.Linq.Dynamic) with Linq to Entities. My linq statement is below.

var query = this.db.Products.AsQueryable()
           .Where(strCondition)
           .OrderBy("ProductNumber")
           .Select("new(ProductNumber, ProductDescription, ProductCategory.Name)");

Now that I have the "query", I don't know how to get the value of each of the field.

        string strTemp;
        foreach (var item in query)
        {
            strTemp = item.?
        }

It's anonymous type so I can't really use strongly type to get the value. What can I do? The reason I select to get anonymous type fields is because I need to get ProductCategory.Name field into the result. Is there a better way to get ProductCategory.Name in the result with Dynamic Expression API? Can anyone help?

A: 

Just use the property name you've used to create your anonymous object. VS intellisense should pick them up.

string strTemp;
        foreach (var item in query)
        {
            strTemp = item.ProductItem;
        }

You can specify your own property names as well:

.Select("new(ProductNumber as Number, ProductDescription as Description, ProductCategory.Name as Name)")
Jakub Konecki
The problem is VS intellisense did not pick it up. I assumed it because the query is using Dynamic Expression API which uses a string value. Any clue?
Seecott
@Seecott - You're right. VS won't pick them up. Take a look here: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Jakub Konecki
I have looked at it before. No answer there. Does anyone know how or has a sample code to get the item properties?
Seecott