DynamicLinq SELECT

Asked By R M
09-Feb-10 02:03 PM
Earn up to 0 extra points for answering this tough question.
Hiya!

I'm trying to do a select on a table where my column names that I select are built off a comma delimited list that I built (this works just fine and produces results like [Field1],[Field2],[Field3] and so on).

So I decided to use the DynamicLinq libraries that MS put out (http://msdn.microsoft.com/en-us/vcsharp/bb894665.aspx) that allow for such operations.

My select query is like this:

dc is my data context

var results =
dc.uf_GetTable(lastUpdated, DateTime.Now).
Where(uf => uf.TableFieldValue.Equals("I")).
Select("new(@0)", dc.ColumnNames<TableName>());

dc.ColumnNames<TableName>() is where I built the list of columns that I want on the select. Reason I'm doing this is because it changes

The dynamic select is as follows:

        public static IQueryable Select(this IQueryable source, string selector, params object[] values)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (selector == null) throw new ArgumentNullException("selector");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
            return source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Select",
                new Type[] { source.ElementType, lambda.Body.Type },
                source.Expression, lambda));}
So I need help figuring out why I keep getting the returned value for my var results as:
SELECT NULL AS [EMPTY]
FROM [dbo].[uf_TableName](@p0, @p1) AS [t0]
WHERE [t0].[TableFieldValue] = @p2
System.Linq.IQueryable {System.Data.Linq.DataQuery}
It SHOULD say:
SELECT [Field1], [Field2], [Field3]
FROM ...

Any clue what I'm doing wrong here? Tips are totally welcome :)

  The Dynamic Select is fine.

[)ia6l0 iii replied to R M
09-Feb-10 03:00 PM
The  usage of the dc.ColumnNames<TableName>() in the below query looks wrong to me. I have not used DL yet, but to my knowledge, that is what looks suspicious.
var results = 
dc.uf_GetTable(lastUpdated, DateTime.Now).
Where(uf => uf.TableFieldValue.Equals("I")).
Select("new(@0)", dc.ColumnNames<TableName>());

You could always pass in a fixed value (hard-coded value) and check if the variable results are getting populated right.
The   public static IQueryable Select(this IQueryable source, string selector, params object[] values) is fine. 




  re: The Dynamic Select is fine.

R M replied to [)ia6l0 iii
09-Feb-10 03:03 PM
Yeah that part is passing in fine - I tested as you recommended and I still get the same:
SELECT NULL AS Empty
(etc)

It's just a function of type <TEntity> to get the column names in a table and returns a string delimited list.
Create New Account