简体   繁体   中英

Dynamic Linq query using

I am trying to use Dynamic linq query for my file filtering. 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. 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

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

 **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)

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

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.

@0 just means: substitute this by the first parameter in params .

(the @ before SubType is not necessary.)

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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