Generate a C# expression that can concatenate two strings together
I'm trying to concatenate two strings together in a dynamic linq expression. The parameter I pass to the function must be a Dictionary<string, object>
. The problem is that The Expression.Add throws me an error because it doesn't know how to add strings.
What I'm trying to achieve:
x => (string)x["FirstName"] + " Something here..."
What I have:
var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
var firstName = Expression.Call(pe, typeof(Dictionary<string, object>).GetMethod("get_Item"), Expression.Constant("FirstName"));
var prop = Expression.Convert(firstName, typeof(string));
var exp = Expression.Add(prop, Expression.Constant(" Something here..."))
c# linq lambda asp.net-core
add a comment |
I'm trying to concatenate two strings together in a dynamic linq expression. The parameter I pass to the function must be a Dictionary<string, object>
. The problem is that The Expression.Add throws me an error because it doesn't know how to add strings.
What I'm trying to achieve:
x => (string)x["FirstName"] + " Something here..."
What I have:
var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
var firstName = Expression.Call(pe, typeof(Dictionary<string, object>).GetMethod("get_Item"), Expression.Constant("FirstName"));
var prop = Expression.Convert(firstName, typeof(string));
var exp = Expression.Add(prop, Expression.Constant(" Something here..."))
c# linq lambda asp.net-core
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18
add a comment |
I'm trying to concatenate two strings together in a dynamic linq expression. The parameter I pass to the function must be a Dictionary<string, object>
. The problem is that The Expression.Add throws me an error because it doesn't know how to add strings.
What I'm trying to achieve:
x => (string)x["FirstName"] + " Something here..."
What I have:
var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
var firstName = Expression.Call(pe, typeof(Dictionary<string, object>).GetMethod("get_Item"), Expression.Constant("FirstName"));
var prop = Expression.Convert(firstName, typeof(string));
var exp = Expression.Add(prop, Expression.Constant(" Something here..."))
c# linq lambda asp.net-core
I'm trying to concatenate two strings together in a dynamic linq expression. The parameter I pass to the function must be a Dictionary<string, object>
. The problem is that The Expression.Add throws me an error because it doesn't know how to add strings.
What I'm trying to achieve:
x => (string)x["FirstName"] + " Something here..."
What I have:
var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x");
var firstName = Expression.Call(pe, typeof(Dictionary<string, object>).GetMethod("get_Item"), Expression.Constant("FirstName"));
var prop = Expression.Convert(firstName, typeof(string));
var exp = Expression.Add(prop, Expression.Constant(" Something here..."))
c# linq lambda asp.net-core
c# linq lambda asp.net-core
edited Nov 26 '18 at 2:11
Cœur
18.7k9110150
18.7k9110150
asked Feb 2 '17 at 2:05
Andoni ZubizarretaAndoni Zubizarreta
4241415
4241415
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18
add a comment |
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18
add a comment |
2 Answers
2
active
oldest
votes
Adding strings is neither one of the types that expressions handles explicitly (as it does for numeric primitives) nor going to work due to an overload of +
(since string
has no such overload), so you need to explicitly defined the method that should be called when overloading:
Expression.Add(
prop,
Expression.Constant(" Something here...")
typeof(string).GetMethod("Concat", new {typeof(string), typeof(string)}))
This makes the overload of string.Concat
that takes two string arguments the method used.
You could also use Expresssion.Call
but this keeps your +
intention explicit (and is what the C# compiler does when producing expressions, for that reason).
add a comment |
Adding and concatenating are completely different processes. When you "add" two strings together, you are concatenating them, since doing a mathematical addition on strings makes no sense.
The best way to concatenate strings is by using String.Concat
. You can use Expression.Call
to generate the method expression:
// Create the parameter expressions
var strA = Expression.Parameter(typeof(string));
var strB = Expression.Parameter(typeof(string));
// Create a method expression for String.Join
var methodInfo = typeof(string).GetMethod("Concat", new { typeof(string), typeof(string) });
var join = Expression.Call(methodInfo, strA, strB);
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings.String.Join(a, b)
will yieldb
.
– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for usingString.Concat
instead ofString.Join
, but I do feel like pointing out thatJoin
andConcat
aren't that different.Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string.String.Join("", "a", "b")
will return"ab"
.
– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41993094%2fgenerate-a-c-sharp-expression-that-can-concatenate-two-strings-together%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Adding strings is neither one of the types that expressions handles explicitly (as it does for numeric primitives) nor going to work due to an overload of +
(since string
has no such overload), so you need to explicitly defined the method that should be called when overloading:
Expression.Add(
prop,
Expression.Constant(" Something here...")
typeof(string).GetMethod("Concat", new {typeof(string), typeof(string)}))
This makes the overload of string.Concat
that takes two string arguments the method used.
You could also use Expresssion.Call
but this keeps your +
intention explicit (and is what the C# compiler does when producing expressions, for that reason).
add a comment |
Adding strings is neither one of the types that expressions handles explicitly (as it does for numeric primitives) nor going to work due to an overload of +
(since string
has no such overload), so you need to explicitly defined the method that should be called when overloading:
Expression.Add(
prop,
Expression.Constant(" Something here...")
typeof(string).GetMethod("Concat", new {typeof(string), typeof(string)}))
This makes the overload of string.Concat
that takes two string arguments the method used.
You could also use Expresssion.Call
but this keeps your +
intention explicit (and is what the C# compiler does when producing expressions, for that reason).
add a comment |
Adding strings is neither one of the types that expressions handles explicitly (as it does for numeric primitives) nor going to work due to an overload of +
(since string
has no such overload), so you need to explicitly defined the method that should be called when overloading:
Expression.Add(
prop,
Expression.Constant(" Something here...")
typeof(string).GetMethod("Concat", new {typeof(string), typeof(string)}))
This makes the overload of string.Concat
that takes two string arguments the method used.
You could also use Expresssion.Call
but this keeps your +
intention explicit (and is what the C# compiler does when producing expressions, for that reason).
Adding strings is neither one of the types that expressions handles explicitly (as it does for numeric primitives) nor going to work due to an overload of +
(since string
has no such overload), so you need to explicitly defined the method that should be called when overloading:
Expression.Add(
prop,
Expression.Constant(" Something here...")
typeof(string).GetMethod("Concat", new {typeof(string), typeof(string)}))
This makes the overload of string.Concat
that takes two string arguments the method used.
You could also use Expresssion.Call
but this keeps your +
intention explicit (and is what the C# compiler does when producing expressions, for that reason).
answered Feb 2 '17 at 3:00
Jon HannaJon Hanna
91.3k9111206
91.3k9111206
add a comment |
add a comment |
Adding and concatenating are completely different processes. When you "add" two strings together, you are concatenating them, since doing a mathematical addition on strings makes no sense.
The best way to concatenate strings is by using String.Concat
. You can use Expression.Call
to generate the method expression:
// Create the parameter expressions
var strA = Expression.Parameter(typeof(string));
var strB = Expression.Parameter(typeof(string));
// Create a method expression for String.Join
var methodInfo = typeof(string).GetMethod("Concat", new { typeof(string), typeof(string) });
var join = Expression.Call(methodInfo, strA, strB);
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings.String.Join(a, b)
will yieldb
.
– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for usingString.Concat
instead ofString.Join
, but I do feel like pointing out thatJoin
andConcat
aren't that different.Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string.String.Join("", "a", "b")
will return"ab"
.
– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
|
show 4 more comments
Adding and concatenating are completely different processes. When you "add" two strings together, you are concatenating them, since doing a mathematical addition on strings makes no sense.
The best way to concatenate strings is by using String.Concat
. You can use Expression.Call
to generate the method expression:
// Create the parameter expressions
var strA = Expression.Parameter(typeof(string));
var strB = Expression.Parameter(typeof(string));
// Create a method expression for String.Join
var methodInfo = typeof(string).GetMethod("Concat", new { typeof(string), typeof(string) });
var join = Expression.Call(methodInfo, strA, strB);
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings.String.Join(a, b)
will yieldb
.
– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for usingString.Concat
instead ofString.Join
, but I do feel like pointing out thatJoin
andConcat
aren't that different.Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string.String.Join("", "a", "b")
will return"ab"
.
– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
|
show 4 more comments
Adding and concatenating are completely different processes. When you "add" two strings together, you are concatenating them, since doing a mathematical addition on strings makes no sense.
The best way to concatenate strings is by using String.Concat
. You can use Expression.Call
to generate the method expression:
// Create the parameter expressions
var strA = Expression.Parameter(typeof(string));
var strB = Expression.Parameter(typeof(string));
// Create a method expression for String.Join
var methodInfo = typeof(string).GetMethod("Concat", new { typeof(string), typeof(string) });
var join = Expression.Call(methodInfo, strA, strB);
Adding and concatenating are completely different processes. When you "add" two strings together, you are concatenating them, since doing a mathematical addition on strings makes no sense.
The best way to concatenate strings is by using String.Concat
. You can use Expression.Call
to generate the method expression:
// Create the parameter expressions
var strA = Expression.Parameter(typeof(string));
var strB = Expression.Parameter(typeof(string));
// Create a method expression for String.Join
var methodInfo = typeof(string).GetMethod("Concat", new { typeof(string), typeof(string) });
var join = Expression.Call(methodInfo, strA, strB);
edited Feb 2 '17 at 6:51
answered Feb 2 '17 at 2:44
Abion47Abion47
6,60821739
6,60821739
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings.String.Join(a, b)
will yieldb
.
– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for usingString.Concat
instead ofString.Join
, but I do feel like pointing out thatJoin
andConcat
aren't that different.Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string.String.Join("", "a", "b")
will return"ab"
.
– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
|
show 4 more comments
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings.String.Join(a, b)
will yieldb
.
– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for usingString.Concat
instead ofString.Join
, but I do feel like pointing out thatJoin
andConcat
aren't that different.Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string.String.Join("", "a", "b")
will return"ab"
.
– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
@Rob Fair point, will edit.
– Abion47
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings. String.Join(a, b)
will yield b
.– Rob♦
Feb 2 '17 at 2:46
String.Join
is different from concatenating strings. String.Join(a, b)
will yield b
.– Rob♦
Feb 2 '17 at 2:46
@Rob I'm figuring out the code for using
String.Concat
instead of String.Join
, but I do feel like pointing out that Join
and Concat
aren't that different. Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string. String.Join("", "a", "b")
will return "ab"
.– Abion47
Feb 2 '17 at 2:50
@Rob I'm figuring out the code for using
String.Concat
instead of String.Join
, but I do feel like pointing out that Join
and Concat
aren't that different. Join
just concatenates an array of strings together separated by a given delimiter, which can be an empty string. String.Join("", "a", "b")
will return "ab"
.– Abion47
Feb 2 '17 at 2:50
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
@Rob So while my current code isn't optimal, it's still entirely usable.
– Abion47
Feb 2 '17 at 2:51
2
2
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
If it's not the best way, then you shouldn't say it's the best way.
– Jon Hanna
Feb 2 '17 at 2:55
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41993094%2fgenerate-a-c-sharp-expression-that-can-concatenate-two-strings-together%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does your Dictionary<string,object> has FirstName as a string? {Key:"FirstName",Value:"Andoni"}?
– Vijay
Feb 2 '17 at 2:18