简体   繁体   English

动态Linq查询使用

[英]Dynamic Linq query using

I am trying to use Dynamic linq query for my file filtering. 我正在尝试使用动态linq查询进行文件过滤。 Basically, I have the user define the WhereExpression and the OrderByExpression strings in an XML document which I read and then apply to a list of files to be sourced to various directions. 基本上,我让用户在我阅读的XML文档中定义WhereExpression和OrderByExpression字符串,然后将其应用于要从各个方向获取的文件列表。 I found a library at 我在找到图书馆

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

And decided to use it but I seem to be having some issues getting started. 并决定使用它,但我似乎在入门方面遇到了一些问题。 So far, when I try to pass the where expression and the Orderby to the IQueryable list e.ge 到目前为止,当我尝试将where表达式和Orderby传递给IQueryable列表e.ge时

 **WhereQuery**="@SubType = 02" 
 **OrderByQuery**="FormID"

      var sortedRepos = Repos.Where(dir.WhereExpression).OrderBy(dir.OrderByExpression);

I get the following error message. 我收到以下错误消息。

System.Linq.Dynamic.ParseException was caught Message=Operator '=' incompatible with operand types 'String' and 'Int32' Source=Dynamic Position=12 StackTrace: at System.Linq.Dynamic.ExpressionParser.CheckAndPromoteOperands(Type signatures, String opName, Expression& left, Expression& right, Int32 errorPos) at System.Linq.Dynamic.ExpressionParser.ParseComparison() at System.Linq.Dynamic.ExpressionParser.ParseLogicalAnd() at System.Linq.Dynamic.ExpressionParser.ParseLogicalOr() at System.Linq.Dynamic.ExpressionParser.ParseExpression() at System.Linq.Dynamic.ExpressionParser.Parse(Type resultType) at System.Linq.Dynamic.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values) 捕获了System.Linq.Dynamic.ParseException Message = Operator'='与操作数类型'String'和'Int32'不兼容Source =动态位置= 12 StackTrace:位于System.Linq.Dynamic.ExpressionParser.CheckAndPromoteOperands(类型签名,字符串opName ,System.Linq.Dynamic.ExpressionParser.ParseComparison()处的System.Linq.Dynamic.ExpressionParser.ParseLogicalAnd()处的System.Linq.Dynamic.ExpressionParser.ParseLogicalOr()处的Expression,left,Expression&right和Int32 errorPos) System.Linq.Dynamic.ExpressionParser.Parse.Type.System.Linq.Dynamic.DynamicExpression.ParseLambda处的.Dynamic.ExpressionParser.ParseExpression()(ParameterExpression []参数,类型resultType,字符串表达式,Object []值)

Now when I do use the comparison operator like so, 现在,当我像这样使用比较运算符时,

 **WhereQuery="@SubType == 02" 
 OrderByQuery="FormID"**

I get the following error as well 我也收到以下错误

System.Linq.Dynamic.ParseException was caught
  Message=Operator '==' incompatible with operand types 'String' and 'Int32'
  Source=Dynamic
  Position=12
  StackTrace:
       at System.Linq.Dynamic.ExpressionParser.CheckAndPromoteOperands(Type signatures, String opName, Expression& left, Expression& right, Int32 errorPos)
       at System.Linq.Dynamic.ExpressionParser.ParseComparison()
       at System.Linq.Dynamic.ExpressionParser.ParseLogicalAnd()
       at System.Linq.Dynamic.ExpressionParser.ParseLogicalOr()
       at System.Linq.Dynamic.ExpressionParser.ParseExpression()
       at System.Linq.Dynamic.ExpressionParser.Parse(Type resultType)
       at System.Linq.Dynamic.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
       at System.Linq.Dynamic.DynamicExpression.ParseLambda(Type itType, Type resultType, String expression, Object[] values)
       at System.Linq.Dynamic.DynamicQueryable.Where(IQueryable source, String predicate, Object[] values)
       at System.Linq.Dynamic.DynamicQueryable.Where[T](IQueryable`1 source, String predicate, Object[] values)

How can I correctly specify my expression so as to filter my list please? 如何正确指定我的表达方式以过滤列表? Thanks in advance 提前致谢

You have to use == instead of = . 您必须使用==而不是= The former is used for comparison, the latter for assignment. 前者用于比较,后者用于分配。

The signature of the Where method is Where方法的签名是

public static IQueryable<T> Where<T>(this IQueryable<T> source, 
                                     string predicate,
                                     params object[] values)

So you can use parameters of the desired type... 因此,您可以使用所需类型的参数...

Where("SubType = @0", "02")

...or... ...要么...

Where("SubType = @0", 2)

...if SubType is a numeric type. ...如果SubType是数字类型。

@0 just means: substitute this by the first parameter in params . @0只是意味着:用params的第一个参数替换它。

(the @ before SubType is not necessary.) (在@SubType是没有必要的。)

When you have this error: 当您遇到此错误时:

  Message=Operator '==' incompatible with operand types 'String' and 'Int32'

It means what it says. 这就是说的话。 To correct that put the value in quotation. 要更正该值,请将该值放在引号中。 ie

WhereQuery**="@SubType = "/"02/"" 

This should work with you.. 这应该与您一起工作..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM