LINQ - DynamicLinq SELECT

Asked By R M
09-Feb-10 02:03 PM
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.  The Dynamic Select is fine.

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. 




  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
help
this IQueryable source, string predicate, object data, params object[] values) { if (source = = null) throw new ArgumentNullException("source"); if (predicate = = null) throw new ArgumentNullException("predicate"); LambdaExpression lambda; RPDBLinqDataContext dc = new RPDBLinqDataContext(); lambda = DynamicExpression.ParseLambda(dc.GetTable(data.GetType()).ElementType, typeof(bool), predicate, values); return dc.GetTable(data.GetType
command< / param> private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) { if( command = = null ) throw new ArgumentNullException( "command" ); if( commandParameters ! = null ) { foreach (SqlParameter p in commandParameters) { if( p ! = null ) { / / Check for derived transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if( command = = null ) throw new ArgumentNullException( "command" ); if( commandText = = null | | commandText.Length = = 0 ) throw new ArgumentNullException( "commandText" ); / / If the provided connection is not open, we will open it if (connection.State CommandType commandType, string commandText, params SqlParameter[] commandParameters) { if( connectionString = = null | | connectionString.Length = = 0 ) throw new ArgumentNullException( "connectionString" ); / / Create & open a SqlConnection, and dispose of it after we are done using (SqlConnection string connectionString, string spName, params object[] parameterValues) { if( connectionString = = null | | connectionString.Length = = 0 ) throw new ArgumentNullException( "connectionString" ); if( spName = = null | | spName.Length = = 0 ) throw new ArgumentNullException( "spName" ); / / If we receive parameter values, we need to figure out where they go if ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { if( connection = = null ) throw new ArgumentNullException( "connection" ); / / Create a command and prepare it for execution SqlCommand cmd = new SqlCommand(); bool mustCloseConnection static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues) { if( connection = = null ) throw new ArgumentNullException( "connection" ); if( spName = = null | | spName.Length = = 0 ) throw new ArgumentNullException( "spName" ); / / If we receive parameter
System.ArgumentNullException .NET Framework This error occurs erratically at startup. It appears to be all native code. Does anyone have any idea what the heck could be cauing this? System.ArgumentNullException: Value cannot be null. Parameter name: pen at System.Drawing.Graphics.DrawLine(Pen pen, Int32 Forms.Control.WndProc (1) System.Windows.Forms.Control.OnPrint (1) System.Drawing.Graphics.DrawLine (1) ArgumentNullException (1) WndProc (1) I have seen an ArgumentNullException generated inside the control painting routines when threading is used incorrectly with windows applications. What parameter to be null. That might give the OP some more clues anyway. keywords: System.ArgumentNullException description: This error occurs erratically at startup. It appears to be all native code. Does
error:System.ArgumentNullException:Value cannot be null.ParameterName Hi, i'm getting error in below code :System.ArgumentNullException:Value cannot be null.ParameterName protected void DisplayFeedback() { try { ConnectionClass cc = new ConnectionClass (); SqlDataReader dr