C# 创建按主键排序的select LINQ查询,而不指定模型类型
我有一个过程,基本上是将数据从一个数据库移动到另一个数据库,为此有一系列消息处理程序。为了让我的数据移动过程是动态的,我想通过在消息处理程序上使用自定义属性来开发一个通用的过程,从而允许我自动驱动数据迁移 每个消息处理程序都有一个自定义属性,例如C# 创建按主键排序的select LINQ查询,而不指定模型类型,c#,linq,.net-core,entity-framework-core,expression-trees,C#,Linq,.net Core,Entity Framework Core,Expression Trees,我有一个过程,基本上是将数据从一个数据库移动到另一个数据库,为此有一系列消息处理程序。为了让我的数据移动过程是动态的,我想通过在消息处理程序上使用自定义属性来开发一个通用的过程,从而允许我自动驱动数据迁移 每个消息处理程序都有一个自定义属性,例如 [BulkImport(EntityModelType=typeof(TblProxyCompany))] 该属性的实现如下所示:- [AttributeUsage(AttributeTargets.Class, AllowMultiple = fal
[BulkImport(EntityModelType=typeof(TblProxyCompany))]
该属性的实现如下所示:-
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class BulkImportAttribute : Attribute
{
public Type EntityModelType { get; set; }
public Type[] DependentEntityModelTypes { get; set; }
}
我的数据迁移过程读取所有具有此属性的消息处理程序,该属性创建我的任务列表。在我的代码中,我循环执行每个任务,并向本地存储队列提交一条消息,这就是我的问题开始的地方
我需要通过模型类型(即TblProxyCompany
)从属性中定义的表中读取一批行,下面的linq查询执行此操作:-
var query = _oltpContext.Query(importItem.EntityModelType)
.Cast<IBulkImport>()
.Skip(skip)
.Take(BATCH_SIZE);
我发现了大量关于创建扩展方法的示例,这些扩展方法允许order by column作为字符串传递,但它们在某种程度上都依赖于传递的泛型类型来指定类型
我发现了下面的例子:-
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
bool desc)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}
public静态IQueryable OrderBy(此IQueryable源,字符串orderByProperty,
布尔描述)
{
string command=desc?“OrderByDescending”:“OrderBy”;
变量类型=类型(强度);
var property=type.GetProperty(orderByProperty);
var参数=表达式参数(类型为“p”);
var propertyAccess=Expression.MakeMemberAccess(参数,属性);
var orderByExpression=Expression.Lambda(propertyAccess,参数);
var resultExpression=Expression.Call(typeof(Queryable),command,新类型[]{Type,property.PropertyType},
source.Expression,Expression.Quote(orderByExpression));
返回source.Provider.CreateQuery(resultExpression);
}
但正如你所看到的,我需要再次通过tenty类型。我认为我的问题是从转换到
IBulkImport
开始的,但我不明白如何构造一个LINQ语句来选择、排序、,当我必须从模型类型开始时,即TblProxyCompany作为BulkImport
custom属性的参数时,获取并选择它。为什么不添加type entityModelType
作为额外参数,并放弃var type=typeof(tenty)代码>?我收到以下错误:System.InvalidOperationException:“System.Linq.Queryable”类型上的“无泛型方法”OrderBy与提供的类型参数和参数兼容。如果方法是非泛型的,则不应提供类型参数。'事实上,我认为它是有效的,我删除了键入的引用,并在我的演员阵容之前移动了订单,我认为它是有效的!!!
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
bool desc)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}