0

I started a project that will allow me to create sql queries using lambda expressions.

for some reason when I'm trying the following code, the lambda injected with "Convert" method please help me understand the reason and how I can avoid it.

GroupBy function:

public QueryBuilder<T> GroupBy(params Expression<Func<T, object>>[] expression)
{
    _groupBy.AddRange(expression);
    return this;
}

Usage example:

query.GroupBy(x=>x.IntExample,x=>x.StringExample)

from the immediate:

 expression
 {System.Linq.Expressions.Expression<System.Func<Tests.Example,object>>[2]}
 [0]: {x => Convert(x.IntExample)}
 [1]: {x => x.StringExample}

Fill free to fork the project: https://github.com/matanshidlov/Lambda-To-Sql/tree/master/Lambda-To-Sql

2
  • try this query.GroupBy(x=> x.IntExample, x.StringExample) Commented Jun 27, 2015 at 16:39
  • I cant do this, that code doesn't pass compilation Commented Jun 27, 2015 at 16:42

1 Answer 1

1

object is a reference type. int is a value type. In order to convert an int into an object, and thus into a reference type, the int has to be boxed. Boxing involves creating a reference type object as a wrapper around the int. This is what Convert does.

See: Boxing and Unboxing (C# Programming Guide)
and also: Expression.Convert Method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.