我需要將下面的代碼轉換爲一個表達式,我會解釋爲什麼:需要幫助建設的LINQ to SQL表達式
results = results.Where(answer => answer.Question.Wording.Contains(term));
結果是IQueryable的<ISurveyAnswer>
問題是ISurveyQuestion
措辭字符串
問題是,問題並不總是LINQ to SQL屬性的名稱。
這會給我的PropertyInfo的實際ISurveyQuestion財產
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();
}
一旦我擁有的PropertyInfo如何翻譯,爲表達式樹?
編輯:
我基本上需要的是:
results = results.Where(answer => answer.GetQuestionProperty().GetValue(answer).Wording.Contains(term));
但是,這並不工作,所以我需要建立表達式樹本人,對LINQ到SQL。
最後,我倆都在ScottGu的博客:) – Kelsey 2009-11-16 19:42:00