views:

174

answers:

3

I need to translate the following Code to an Expression and I will explain why:

results = results.Where(answer => answer.Question.Wording.Contains(term));

results is IQueryable<ISurveyAnswer>
Question is ISurveyQuestion
Wording is String

The problem is, Question is not always the name of the LINQ to SQL property.

This will give me the PropertyInfo for the actual ISurveyQuestion property

private static PropertyInfo FindNaturalProperty<TMemberType>(Type search)
{
    IDictionary<string,PropertyInfo> properties = new Dictionary<string,PropertyInfo>();

    search.GetProperties().Each(prop =>
    {
        if (null != prop.PropertyType.GetInterface(typeof(TMemberType).Name))
            properties.Add(prop.Name, prop);
    });

    if (properties.Count < 1) throw new ArgumentException(String.Format("{0} has no properties of type {1}", search.Name, typeof(TMemberType).Name));
    if (properties.Count == 1) return properties.Values.First();

    search.GetInterfaces().Each(inter =>
    {
        inter.GetProperties().Each(prop =>
        {
            if (null != prop.PropertyType.GetInterface(typeof(TMemberType).Name))
                properties.Remove(prop.Name);
        });
    });

    if (properties.Count < 1) throw new ArgumentException(String.Format("{0} has no properties of type {1} that are not members of an interface", search.Name, typeof(TMemberType).Name));
    if (properties.Count > 1) throw new AmbiguousMatchException(String.Format("{0} has more than one property that are of type {1} and are not members of an interface", search.Name, typeof(TMemberType).Name));


    return properties.Values.First();
}

Once I have the PropertyInfo how do I translate that to an Expression Tree?

EDIT:

What I basically need is:

results = results.Where(answer => answer.GetQuestionProperty().GetValue(answer).Wording.Contains(term));

But that doesn't work so I need to build the Expression Tree myself, for linq-to-sql.

A: 

http://www.linqpad.net/

linqpad will convert it for you.

John Boker
+1  A: 

Reading the question I think what you're after is Dynamic Linq - which is a helper library to let you build Linq queries dynamically (!) using strings as opposed to at design time. That means that if you can get your property name you should be able create your query on the fly.

ScottGu has an article here

Murph
We both ended up on ScottGu's blog :)
Kelsey
+1  A: 

What your trying to do is create a dynamic query and you want the action tables / properties your query against to be dynamic as well. I am not sure if this is easily possible based on how you want to use it.

Check out ScottGu's blog post: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

and

Check out Rick Strahl's blog post: http://www.west-wind.com/Weblog/posts/143814.aspx

Kelsey
Ah Ha! This definitely looks like the answer. Thank you!Unfortunately there is only one Green check mark and Murph answered first :(
Joe Flateau
results.ElementType should give me access to the PropertyInfo I am looking for. Then DynamicLinq should build the Expression for me... I think.
Joe Flateau