AFAIK, you should build the lambda to be passed to the Select().

This should not be too complex, something like:
PropertyInfo property = typeof(T).GetProperty(propertyName);
ParameterExpression p = Expression.Parameter(typeof(T), "p");
Expression body = Expression.MemberAccess(p, property);
LambdaExpression forSelect = Expression.Lambda(body, p);

This, passed to the Select() method, should be what you need.

BTW, let me say that I do not see any reason to use such method, unless you
*really* need to optimize the bandwith between your rdbms and the
application server...


Giacomo

On Mon, Jun 29, 2009 at 7:28 AM, greyman <[email protected]> wrote:

>
> Hi,
>
> I have a scenario where I want to select a single property of one of
> my mapped classes. The tricky bit is I want to be able to specify the
> property at runtime.
>
> I have sort of worked out how to do it using the method below but the
> problem is that I get an exception fairly regularly about the
> enumerator being not ready. Has anyone done a similar thing and have a
> more robust approach or any suggestions?
>
> public override object GetProperty(Guid id, string propertyName) {
>  try {
>    PropertyInfo property = typeof(T).GetProperty(propertyName);
>    if (property == null) {
>      throw new ApplicationException("Property " + propertyName + "
> for " + typeof(T).Name + " could not be found, GetProperty failed.");
>    }
>    else {
>      using (DataContext dataContext =
> DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
>        Table<T> table = dataContext.GetTable<T>();
>        string idString = id.ToString();
>        IEnumerator result = (from asset in table where asset.Id ==
> idString select asset).Select("new (" + propertyName +
> ")").GetEnumerator();
>        result.MoveNext();
>        var resultObj = result.Current;
>        PropertyInfo resultProperty = resultObj.GetType().GetProperty
> (propertyName);
>        return resultProperty.GetValue(resultObj, null);
>      }
>    }
>  }
>  catch (Exception excp) {
>    logger.Error("Exception DBLinqAssetPersistor GetProperty (for " +
> typeof(T).Name + "). " + excp.Message);
>    throw;
>  }
> }
>
> Thanks,
>
> Aaron
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to