在我当前的项目中,存储库使用了 EntityFramework,其中包含许多表作为 News。所以每次,我们都有一个新的输入函数,我们创建相关的数据库,生成 edmx,在存储库中创建一个新的方法。示例:db.News.Where(c=>c.Name=='myNews'); db.Articles.Where(c=>c.ArticleName=='myArticle');
我寻找一种方法来避免每次都写一个非常相似的代码。
我使用 Generic 创建了这段代码。所以这是一个代码示例,想法没问题,我可以首先通过propertyName和字符串搜索来创建一个标准方法。有用。
宣言
public class SampleClass
{
public string SomeField = "Test";
}
public class ConfSampleClass2
{
public Type Type { get; set; }
}
来电:
IQueryable result = GetCompaniesA<SampleClass2>("Name", "alpine");
方法 :
public IQueryable GetCompaniesA<T>(string propertyName, string search)
{
try
{
IQueryable<T> results = null;
SampleClass2 sc2 = new SampleClass2();
List<SampleClass2> listSc2 = new List<SampleClass2>();
listSc2.Add(new SampleClass2() { Name = "alpine" });
listSc2.Add(new SampleClass2() { Name = "Statue" });
listSc2.Add(new SampleClass2() { Name = "Gateau" });
//listSc2.Where(c => c.Name == "alpine");
IQueryable<SampleClass2> queryableData = listSc2.AsQueryable<SampleClass2>();
// Compose the expression tree that represents the parameter to the predicate.
ParameterExpression pe = Expression.Parameter(typeof(T),typeof(T).Name);
Expression member = Expression.Property(pe, typeof(T).GetProperty(propertyName));
Expression left = Expression.Call(member, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
Expression right = Expression.Constant(search);
Expression e1 = Expression.Equal(left, right);
MethodCallExpression whereCallExpression =
Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<T, bool>>(e1, new ParameterExpression[] { pe }));
return queryableData.Provider.CreateQuery(whereCallExpression);
}
catch (Exception ex)
{
throw ex;
}
}
但我想更进一步,避免使用泛型但使用动态来避免名称耦合。所以我修改了获取这个的方法:
来电:
Console.WriteLine(GetCompaniesDynamic(new ConfSampleClass2(){ Type=typeof(SampleClass2) },"Name", "alpine"));
方法 :
public IQueryable GetCompaniesDynamic(dynamic dynamicObj, string propertyName, string search)
{
try
{
IQueryable results = null;
SampleClass2 sc2 = new SampleClass2();
List<SampleClass2> listSc2 = new List<SampleClass2>();
listSc2.Add(new SampleClass2() { Name = "alpine" });
listSc2.Add(new SampleClass2() { Name = "Statue" });
listSc2.Add(new SampleClass2() { Name = "Gateau" });
IQueryable<SampleClass2> queryableData = listSc2.AsQueryable<SampleClass2>();
ParameterExpression pe = Expression.Parameter(dynamicObj.Type, dynamicObj.Type.Name);
Expression member = Expression.Property(pe, dynamicObj.Type.GetProperty(propertyName));
Expression left = Expression.Call(member, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
Expression right = Expression.Constant(search);
Expression e1 = Expression.Equal(left, right);
//the issue appears on this line ParameterExpression of type //'EntityFrameworkGeolocationExpression.SampleClass2' cannot be used for delegate parameter //of type 'System.Object', I can't compile if I replace Object by Dynamic.
MethodCallExpression whereCallExpression =
Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<Object, bool>>(e1, new ParameterExpression[] { pe }));
return results = queryableData.Provider.CreateQuery(whereCallExpression);
}
catch (Exception ex)
{
throw ex;
}
}
我试图找到一种真正传递动态对象的方法,因此在我的存储库中,我将用表达式三替换用简单查询调用的新闻、文章和对象。我找到了很多主题,并且与某人一起阅读它是不可能的. 我的下一步是通过演化参数来演化表达式三。
你能帮我更进一步吗?
最好的问候, 亚历山大