Linq to sql 需要帮助构建LINQ到SQL表达式吗
我需要将以下代码转换为表达式,并解释原因:Linq to sql 需要帮助构建LINQ到SQL表达式吗,linq-to-sql,lambda,expression-trees,Linq To Sql,Lambda,Expression Trees,我需要将以下代码转换为表达式,并解释原因: results = results.Where(answer => answer.Question.Wording.Contains(term)); 结果是可查询的 问题是调查问题 措辞是字符串 问题是,问题并不总是LINQtoSQL属性的名称 这将为我提供实际IsSurveyQuestion属性的PropertyInfo private static PropertyInfo FindNaturalProperty<TMemberTyp
results = results.Where(answer => answer.Question.Wording.Contains(term));
结果是可查询的问题是调查问题
措辞是字符串 问题是,问题并不总是LINQtoSQL属性的名称 这将为我提供实际IsSurveyQuestion属性的PropertyInfo
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();
}
但这不起作用,所以我需要自己构建表达式树,用于linq到sql。
linqpad将为您转换它。阅读此问题,我认为您所追求的是动态Linq-这是一个帮助程序库,允许您使用字符串动态(!)构建Linq查询,而不是在设计时。这意味着,如果您可以获取属性名称,您应该能够动态创建查询
ScottGu有一篇文章您要做的是创建一个动态查询,并且您希望查询所针对的操作表/属性也是动态的。我不确定根据您想要如何使用它,这是否容易实现 查看ScottGu的博客帖子: 及 查看Rick Strahl的博客帖子:
我们最后都上了ScottGu的博客:)啊哈!这看起来确实是答案。非常感谢。不幸的是,只有一个绿色的复选标记,Murph首先回答:(results.ElementType应该让我访问我正在寻找的属性信息。然后DynamicLink应该为我构建表达式……我想。
results = results.Where(answer => answer.GetQuestionProperty().GetValue(answer).Wording.Contains(term));