[英]Generate Dynamic Linq query using outerIt
I am using Microsoft's Dynamic Linq (System.Linq.Dynamic) library to generate some queries at run time. 我正在使用Microsoft的Dynamic Linq(System.Linq.Dynamic)库在运行时生成一些查询。 This has worked great for me, but for one specific scenario.
这对我来说非常有效,但是对于一种特定情况而言。
Simplified scenario - I am trying to query all claims which have some specific tags that the user has selected and whose Balance is greater than some number. 简化方案-我正在尝试查询所有索赔,这些索赔具有用户选择的某些特定标签并且其余额大于某个数字。
static void Main(string[] args)
{
var claims = new List<Claim>();
claims.Add(new Claim { Balance = 100, Tags = new List<string> { "Blah", "Blah Blah" } });
claims.Add(new Claim { Balance = 500, Tags = new List<string> { "Dummy Tag", "Dummy tag 1" } });
// tags to be searched for
var tags = new List<string> { "New", "Blah" };
var parameters = new List<object>();
parameters.Add(tags);
var query = claims.AsQueryable().Where("Tags.Any(@0.Contains(outerIt)) AND Balance > 100", parameters.ToArray());
}
public class Claim
{
public decimal? Balance { get; set; }
public List<string> Tags { get; set; }
}
This query throws an error: 该查询引发错误:
An unhandled exception of type 'System.Linq.Dynamic.ParseException' occurred in System.Linq.Dynamic.dll Additional information: No property or field 'Balance' exists in type 'String'
System.Linq.Dynamic.dll中发生类型为'System.Linq.Dynamic.ParseException'的未处理异常附加信息:类型'String'中不存在属性或字段'Balance'
Dynamic linq parser seems to try to find the Balance property on the Tag and not on the Claim object. 动态linq解析器似乎试图在Tag而不是Claim对象上找到Balance属性。
I might be missing something or this could be a bug in the library. 我可能丢失了一些东西,或者这可能是库中的错误。
I would really appreciate if someone could help me with this. 如果有人可以帮助我,我将非常感激。
Found a/the bug in ParseAggregate ... The pushing of it
→ outerIt
and back doesn't work if there are multiple levels. 发现A /臭虫ParseAggregate ...的推动
it
→ outerIt
和回来,如果有多个级别不起作用。 The code supposes that the it
and outerIt
won't be changed by a third party before being reset (technically the code isn't reentrant). 该代码假定
it
和outerIt
不会在重置之前被第三方更改(从技术上讲该代码不可重入)。 You can try with other variants of System.Linq.Dynamic
(there are like two or three variants out of there). 您可以尝试使用
System.Linq.Dynamic
其他变体(那里有两个或三个变体)。 Probably some variants have already fixed it. 可能有一些变体已将其修复。
Or you can take the code from the linked site and recompile it inside your code (in the end the "original" System.Linq.Dynamic is a single cs file) and you can patch it like this: 或者,您可以从链接的站点获取代码,然后在代码内部对其进行重新编译(最后,“原始” System.Linq.Dynamic是单个cs文件),您可以像这样对其进行修补:
Expression ParseAggregate(Expression instance, Type elementType, string methodName, int errorPos)
{
// Change starts here
var originalIt = it;
var originalOuterIt = outerIt;
// Change ends here
outerIt = it;
ParameterExpression innerIt = Expression.Parameter(elementType, elementType.Name);
it = innerIt;
Expression[] args = ParseArgumentList();
// Change starts here
it = originalIt;
outerIt = originalOuterIt;
// Change ends here
MethodBase signature;
if (FindMethod(typeof(IEnumerableSignatures), methodName, false, args, out signature) != 1)
I've already opened an Issue with the suggested bug fix in the github of the project. 我已经在项目的github中打开了一个带有建议的错误修复的问题。
This seems to be working correctly in my version: System.Linq.Dynamic.Core 在我的版本中,这似乎正常工作: System.Linq.Dynamic.Core
See the test here: https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/ComplexTests.cs#L19 在此处查看测试: https : //github.com/StefH/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/ComplexTests.cs#L19
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.