Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建按主键排序的select LINQ查询,而不指定模型类型_C#_Linq_.net Core_Entity Framework Core_Expression Trees - Fatal编程技术网

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);
}