提问人:SpyrosT 提问时间:9/26/2023 最后编辑:SpyrosT 更新时间:9/26/2023 访问量:64
IQueryable 创建查询失败,并出现错误 升级到 .net 6 和 ef 6 后,无法将类型为“System.Object”的表达式用于参数
IQueryable create query fails with error Expression of type 'System.Object' cannot be used for parameter after upgrading to .net 6 and ef 6
问:
我正在调用一大块代码 `
var m = typeof(IQueryProvider).GetMethods().First(x => x.Name == "CreateQuery" && x.IsGenericMethod);
var gm = m.MakeGenericMethod(queryType);
var a = (IQueryable)gm.Invoke(provider, new object[] { expression });
提供商来自哪里
(from p in Context.Set<PointOfInterest>() select p).Provider
查询类型为 PointOfInterestDto
表达式是
.Call System.Linq.Queryable.Where(
.Call System.Linq.Queryable.Select(
.Extension<Microsoft.EntityFrameworkCore.Query.QueryRootExpression>,
'(.Lambda #Lambda1<System.Func`2[BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest,BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto]>))
,
'(.Lambda #Lambda2<System.Func`2[BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto,System.Boolean]>))
.Lambda #Lambda1<System.Func`2[BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest,BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto]>(BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest $pointOfInterest)
{
.New BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto(){
Name = $pointOfInterest.Name,
Id = $pointOfInterest.Id,
ExtraFields = (System.Collections.Generic.IDictionary`2[System.String,System.Object])$pointOfInterest.ExtraFields
}
}
.Lambda #Lambda2<System.Func`2[BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto,System.Boolean]>(BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto $o)
{
.Call System.Linq.Enumerable.Any(
(System.Collections.Generic.IEnumerable`1[System.Object]).Call ($o.ExtraFields).get_Item("coffeeChannels"),
.Lambda #Lambda3<System.Func`2[System.Object,System.Boolean]>)
}
.Lambda #Lambda3<System.Func`2[System.Object,System.Boolean]>(System.Object $o) {
.Call ((System.Collections.Generic.IDictionary`2[System.String,System.Object])$o).ContainsKey("id") && (System.Guid).Call ((System.Collections.Generic.IDictionary`2[System.String,System.Object])$o).get_Item("id")
== .Constant<System.Guid>(d5471682-df51-480b-a367-35b7a918f172)
}
调用后,可查询对象会给出这种错误
Expression of type 'System.Object' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[System.Object]' of method 'System.Linq.IQueryable`1[System.Object] AsQueryable[Object](System.Collections.Generic.IEnumerable`1[System.Object])' (Parameter 'arg0')
请记住,extrafields 属性是一个 IDictionary<string, object> ,在这种情况下,coffeeChannel 属性有一个 elemets 数组,我想在其中搜索。 此外,这在以前的版本(ef5、.net5)上也工作正常
我在 sym 表中的所有库中都放置了一个调试器,以逐步了解 CreateQuery 内部发生的事情,但 VS 无法介入。
我仔细检查了表情,看起来不错。
我交叉检查了几个重大更改,但没有运气(也许我错过了一些东西)。
答: 暂无答案
评论
CreateQuery
provider
queryType
expression