[英]Linq To Entities Where Condition to Expressions comparison
我有3个用Entity Framework映射的类
public class A
{
public string Name { get; set; }
}
public class B
{
public string Name { get; set; }
public A A { get; set; }
}
public class C
{
public string Name { get; set; }
}
我有此条件才能到达实体
return queryableOfB.Where(b => b.A.Name = instanceOfC.Name);
因为这是我逻辑中的重复方法,所以我想创建一个方法为:
protected void GetFilter<B, TBProperty, TCProperty>(
IQueryable<B> queryofB, C cModel,
Expression<Func<B, TBProperty>> bExpression,
Expression<Func<C, TCProperty>> cExpression)
{
var bExpValue = cExpression.Compile()(cModel);
queryofB.Where(b => b.Property.EndsWith(bExpValue)); // How can I compare two expressions? but adding "for example" an .EndsWith to expression 1?
}
重要的是不要在表达式中传递.EndsWith,因为必须在方法中完成使用EndsWith,StartsWith,Contains或精确比较的决定。
预先感谢古鲁斯。
实际上where
方法期望的功能是谓词。 您可以尝试以下一种。
仅调用where扩展方法就无济于事。 最好在那里使用方法内联。 但是告诉你你怎么做。
static IEnumerable<T1> M<T1>(IEnumerable<T1> objEnumerable, Func<T1, bool> wherePredicate)
{
return objEnumerable.Where(predicate);
}
// calling like
var listOfBObjects = GetBObjectIEnumerable();
C instanceOfC = GetCClassObject();
M<B>(listOfBObjects, b => b.A.Name = instanceOfC.Name);
更新
您可以使用
M<B>(listOfBObjects, b => b.A.Name.Equals(instanceOfC.Name));
M<B>(listOfBObjects, b => b.A.Name.StartsWith(instanceOfC.Name));
M<B>(listOfBObjects, b => b.A.Name.Contains(instanceOfC.Name));
来改善整体性能。 是的,您可以使用IQueryable
而不是IEnumerable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.