[英]Dynamic linq not correctly apply on list of DynamicClass
I'm trying to use dynamic linq over DynamicClass list.我正在尝试在DynamicClass列表上使用动态 linq 。 From external source I'm getting field.Name = "firstName"
and field.Value = "firstValue"
.从外部来源我得到field.Name = "firstName"
和field.Value = "firstValue"
。
var dynamicIndexes = new List<DynamicClass>();
var props = new DynamicProperty[]
{
new DynamicProperty(field.Name, typeof(string))
};
Type type = DynamicClassFactory.CreateType(props);
var dynamicClass = Activator.CreateInstance(type) as DynamicClass;
dynamicClass.SetDynamicPropertyValue<string>(field.Name, field.Value);
dynamicIndexes.Add(dynamicClass);
var query = dynamicIndexes.AsQueryable();
bool isValid = query.Any("firstName eq \"firstValue\"");
But I'm getting isValid
as false
.但我得到isValid
作为false
。
I did following tests to understand the issue:我做了以下测试来理解这个问题:
added following code line at the beginning, to overwrite the value and then I'm getting isValid
as true
.在开头添加以下代码行,以覆盖该值,然后我将isValid
设置为true
。
field.Value = "firstValue";
but if I change that as follows then I'm getting isValid
as false
.但是如果我将其更改如下,那么我将isValid
为false
。
byte[] utfBytes = Encoding.Default.GetBytes("firstValue");
field.Value = Encoding.Default.GetString(utfBytes);
What I'm missing?我错过了什么? is this issue related to encoding?这个问题与编码有关吗?
Update更新
As suggested here this is a limitation with dynamic linq .正如这里所建议的,这是动态 linq的限制。 Can you suggest an alternative solution/library to filter DynamicClass list with a query expression.您能否建议一个替代解决方案/库来使用查询表达式过滤 DynamicClass 列表。
The bug (and there seems to be a bug with the dynamic link expressions) is not related to encodings.该错误(并且动态链接表达式似乎存在错误)与编码无关。 There are not even different encodings used by the code in the question;问题中的代码甚至没有使用不同的编码; just one encoding: the default encoding.只有一种编码:默认编码。 The code in the question does not demonstrate a scenario where different encodings are being used or otherwise changed between, so i don't know how the asker could jump to the conclusion of different encodings being at play here...问题中的代码没有演示使用不同编码或以其他方式更改编码的场景,所以我不知道提问者如何得出不同编码在这里发挥作用的结论......
Rather, the bug seems to be related to whether strings are being interned or not.相反,该错误似乎与字符串是否被实习有关。 query.Any("firstName eq \"firstValue\"");
will only work as expected and yield true , if the string instance in firstName is interned .如果firstName中的字符串实例是interned ,则只会按预期工作并产生true 。
If the string is not interned (which is typically the case for string dynamically/programmatically created during the runtime of the program), even if its value is precisely "firstValue", the result will be false , regardless of the actual value of the string.如果字符串没有被保留(这通常是在程序运行期间动态/以编程方式创建的字符串的情况),即使它的值恰好是“firstValue”,结果也会是false ,而不管字符串的实际值如何.
The same bug/effect can be demonstrated without involving any text encoding shenanigans at all by replacing the Encoding.Default.GetBytes/Encoding.Default.GetString combo with:通过将 Encoding.Default.GetBytes/Encoding.Default.GetString 组合替换为:
var cc = "firstValue".ToCharArray();
field.Value = new string(cc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.