Linq where expressions as values to add
I have a question about formatting LINQ query
I have the same query with different where options
For example:
var result = context.table.Include(x => x.table2).Where(q => q1);
The query is the same but in different situations I have different where expressions like q1,q2,q3
Can I configure the q objects separately and use it in the query like
var q1 = (q => q.x == "something");
var q2 = (q => q.x == "something2");
var q3 = (q => q.x == "something3");
var qx;
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(q => qx);
c# linq .net-4.5
add a comment |
I have a question about formatting LINQ query
I have the same query with different where options
For example:
var result = context.table.Include(x => x.table2).Where(q => q1);
The query is the same but in different situations I have different where expressions like q1,q2,q3
Can I configure the q objects separately and use it in the query like
var q1 = (q => q.x == "something");
var q2 = (q => q.x == "something2");
var q3 = (q => q.x == "something3");
var qx;
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(q => qx);
c# linq .net-4.5
1
Why not try it out? Basically you can do this, however you have to declareqxoutside the if-statements.
– HimBromBeere
Nov 13 '18 at 9:11
1
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not put them all asORcondition
– Rahul
Nov 13 '18 at 9:19
1
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21
add a comment |
I have a question about formatting LINQ query
I have the same query with different where options
For example:
var result = context.table.Include(x => x.table2).Where(q => q1);
The query is the same but in different situations I have different where expressions like q1,q2,q3
Can I configure the q objects separately and use it in the query like
var q1 = (q => q.x == "something");
var q2 = (q => q.x == "something2");
var q3 = (q => q.x == "something3");
var qx;
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(q => qx);
c# linq .net-4.5
I have a question about formatting LINQ query
I have the same query with different where options
For example:
var result = context.table.Include(x => x.table2).Where(q => q1);
The query is the same but in different situations I have different where expressions like q1,q2,q3
Can I configure the q objects separately and use it in the query like
var q1 = (q => q.x == "something");
var q2 = (q => q.x == "something2");
var q3 = (q => q.x == "something3");
var qx;
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(q => qx);
c# linq .net-4.5
c# linq .net-4.5
edited Nov 13 '18 at 9:56
Tsahi Asher
1,133822
1,133822
asked Nov 13 '18 at 9:08
Leon BarkanLeon Barkan
2,02321028
2,02321028
1
Why not try it out? Basically you can do this, however you have to declareqxoutside the if-statements.
– HimBromBeere
Nov 13 '18 at 9:11
1
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not put them all asORcondition
– Rahul
Nov 13 '18 at 9:19
1
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21
add a comment |
1
Why not try it out? Basically you can do this, however you have to declareqxoutside the if-statements.
– HimBromBeere
Nov 13 '18 at 9:11
1
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not put them all asORcondition
– Rahul
Nov 13 '18 at 9:19
1
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21
1
1
Why not try it out? Basically you can do this, however you have to declare
qx outside the if-statements.– HimBromBeere
Nov 13 '18 at 9:11
Why not try it out? Basically you can do this, however you have to declare
qx outside the if-statements.– HimBromBeere
Nov 13 '18 at 9:11
1
1
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not put them all as
OR condition– Rahul
Nov 13 '18 at 9:19
Why not put them all as
OR condition– Rahul
Nov 13 '18 at 9:19
1
1
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21
add a comment |
4 Answers
4
active
oldest
votes
Not precisely. You would need to do something like
var result = context.table.Include(x => x.table2).Where(qx);
because q1, q2 etc. are already expressions compatible with what .Where() expects.
add a comment |
IQueryable.Where accepts an Expression<Func<TSource, bool>>, you can do something like this:
Expression<Func<SourceType, bool>> qx = q => false;
Expression<Func<SourceType, bool>> q1 = q => q.x == "something";
add a comment |
Just to note before my answer, you can't declare qx like this:
var qx;
because the compiler won't resolve a type right away.
And what you are asking is possible if you specify the type that q is.
Func<type, bool> q1 = (type q) => q.x == "something";
Func<type, bool> q2 = (type q) => q.x == "something2";
Func<type, bool> q3 = (type q) => q.x == "something3";
Func<type, bool> qx = q3; //this needs a value (if x and y are false)
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(qx);
Thevarissue is my fault. Initially it was declared inside theifblocks, making it out of scope for the last line, so I moved it Up. I don't usevarvery often.
– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.varis really meant for anonymous classes and querys, although nothing wrong with it if you can read it
– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
add a comment |
You seem to need a query where the actual condition is provided at runtime. Just use a Predicate:
Predicate<MyType> predicate;
if(x)
{
predicate = q => q.x == "something";
}
else if(y)
{
predicate = q => q.x == "something2";
}
else
predicate = q => q.x == "something3";
var result = context.table.Include(x => x.table2).where(predicate);
add a comment |
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%2f53277384%2flinq-where-expressions-as-values-to-add%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not precisely. You would need to do something like
var result = context.table.Include(x => x.table2).Where(qx);
because q1, q2 etc. are already expressions compatible with what .Where() expects.
add a comment |
Not precisely. You would need to do something like
var result = context.table.Include(x => x.table2).Where(qx);
because q1, q2 etc. are already expressions compatible with what .Where() expects.
add a comment |
Not precisely. You would need to do something like
var result = context.table.Include(x => x.table2).Where(qx);
because q1, q2 etc. are already expressions compatible with what .Where() expects.
Not precisely. You would need to do something like
var result = context.table.Include(x => x.table2).Where(qx);
because q1, q2 etc. are already expressions compatible with what .Where() expects.
answered Nov 13 '18 at 9:12
Tsahi AsherTsahi Asher
1,133822
1,133822
add a comment |
add a comment |
IQueryable.Where accepts an Expression<Func<TSource, bool>>, you can do something like this:
Expression<Func<SourceType, bool>> qx = q => false;
Expression<Func<SourceType, bool>> q1 = q => q.x == "something";
add a comment |
IQueryable.Where accepts an Expression<Func<TSource, bool>>, you can do something like this:
Expression<Func<SourceType, bool>> qx = q => false;
Expression<Func<SourceType, bool>> q1 = q => q.x == "something";
add a comment |
IQueryable.Where accepts an Expression<Func<TSource, bool>>, you can do something like this:
Expression<Func<SourceType, bool>> qx = q => false;
Expression<Func<SourceType, bool>> q1 = q => q.x == "something";
IQueryable.Where accepts an Expression<Func<TSource, bool>>, you can do something like this:
Expression<Func<SourceType, bool>> qx = q => false;
Expression<Func<SourceType, bool>> q1 = q => q.x == "something";
answered Nov 13 '18 at 9:23
mshwfmshwf
2,13742049
2,13742049
add a comment |
add a comment |
Just to note before my answer, you can't declare qx like this:
var qx;
because the compiler won't resolve a type right away.
And what you are asking is possible if you specify the type that q is.
Func<type, bool> q1 = (type q) => q.x == "something";
Func<type, bool> q2 = (type q) => q.x == "something2";
Func<type, bool> q3 = (type q) => q.x == "something3";
Func<type, bool> qx = q3; //this needs a value (if x and y are false)
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(qx);
Thevarissue is my fault. Initially it was declared inside theifblocks, making it out of scope for the last line, so I moved it Up. I don't usevarvery often.
– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.varis really meant for anonymous classes and querys, although nothing wrong with it if you can read it
– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
add a comment |
Just to note before my answer, you can't declare qx like this:
var qx;
because the compiler won't resolve a type right away.
And what you are asking is possible if you specify the type that q is.
Func<type, bool> q1 = (type q) => q.x == "something";
Func<type, bool> q2 = (type q) => q.x == "something2";
Func<type, bool> q3 = (type q) => q.x == "something3";
Func<type, bool> qx = q3; //this needs a value (if x and y are false)
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(qx);
Thevarissue is my fault. Initially it was declared inside theifblocks, making it out of scope for the last line, so I moved it Up. I don't usevarvery often.
– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.varis really meant for anonymous classes and querys, although nothing wrong with it if you can read it
– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
add a comment |
Just to note before my answer, you can't declare qx like this:
var qx;
because the compiler won't resolve a type right away.
And what you are asking is possible if you specify the type that q is.
Func<type, bool> q1 = (type q) => q.x == "something";
Func<type, bool> q2 = (type q) => q.x == "something2";
Func<type, bool> q3 = (type q) => q.x == "something3";
Func<type, bool> qx = q3; //this needs a value (if x and y are false)
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(qx);
Just to note before my answer, you can't declare qx like this:
var qx;
because the compiler won't resolve a type right away.
And what you are asking is possible if you specify the type that q is.
Func<type, bool> q1 = (type q) => q.x == "something";
Func<type, bool> q2 = (type q) => q.x == "something2";
Func<type, bool> q3 = (type q) => q.x == "something3";
Func<type, bool> qx = q3; //this needs a value (if x and y are false)
if(x)
{
qx = q1;
}
if(y)
{
qx = q2;
}
var result = context.table.Include(x => x.table2).Where(qx);
answered Nov 13 '18 at 10:18
Daniel LoudonDaniel Loudon
678116
678116
Thevarissue is my fault. Initially it was declared inside theifblocks, making it out of scope for the last line, so I moved it Up. I don't usevarvery often.
– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.varis really meant for anonymous classes and querys, although nothing wrong with it if you can read it
– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
add a comment |
Thevarissue is my fault. Initially it was declared inside theifblocks, making it out of scope for the last line, so I moved it Up. I don't usevarvery often.
– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.varis really meant for anonymous classes and querys, although nothing wrong with it if you can read it
– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
The
var issue is my fault. Initially it was declared inside the if blocks, making it out of scope for the last line, so I moved it Up. I don't use var very often.– Tsahi Asher
Nov 13 '18 at 12:28
The
var issue is my fault. Initially it was declared inside the if blocks, making it out of scope for the last line, so I moved it Up. I don't use var very often.– Tsahi Asher
Nov 13 '18 at 12:28
You should explicity write the type, its best practice.
var is really meant for anonymous classes and querys, although nothing wrong with it if you can read it– Daniel Loudon
Nov 13 '18 at 12:34
You should explicity write the type, its best practice.
var is really meant for anonymous classes and querys, although nothing wrong with it if you can read it– Daniel Loudon
Nov 13 '18 at 12:34
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
I totally agree. That's why I don't use it often.
– Tsahi Asher
Nov 13 '18 at 13:40
add a comment |
You seem to need a query where the actual condition is provided at runtime. Just use a Predicate:
Predicate<MyType> predicate;
if(x)
{
predicate = q => q.x == "something";
}
else if(y)
{
predicate = q => q.x == "something2";
}
else
predicate = q => q.x == "something3";
var result = context.table.Include(x => x.table2).where(predicate);
add a comment |
You seem to need a query where the actual condition is provided at runtime. Just use a Predicate:
Predicate<MyType> predicate;
if(x)
{
predicate = q => q.x == "something";
}
else if(y)
{
predicate = q => q.x == "something2";
}
else
predicate = q => q.x == "something3";
var result = context.table.Include(x => x.table2).where(predicate);
add a comment |
You seem to need a query where the actual condition is provided at runtime. Just use a Predicate:
Predicate<MyType> predicate;
if(x)
{
predicate = q => q.x == "something";
}
else if(y)
{
predicate = q => q.x == "something2";
}
else
predicate = q => q.x == "something3";
var result = context.table.Include(x => x.table2).where(predicate);
You seem to need a query where the actual condition is provided at runtime. Just use a Predicate:
Predicate<MyType> predicate;
if(x)
{
predicate = q => q.x == "something";
}
else if(y)
{
predicate = q => q.x == "something2";
}
else
predicate = q => q.x == "something3";
var result = context.table.Include(x => x.table2).where(predicate);
edited Nov 13 '18 at 9:19
answered Nov 13 '18 at 9:14
HimBromBeereHimBromBeere
23.1k43159
23.1k43159
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53277384%2flinq-where-expressions-as-values-to-add%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
1
Why not try it out? Basically you can do this, however you have to declare
qxoutside the if-statements.– HimBromBeere
Nov 13 '18 at 9:11
1
Why not just assign the value of the string?
– mshwf
Nov 13 '18 at 9:11
Why not put them all as
ORcondition– Rahul
Nov 13 '18 at 9:19
1
@Rahul That´s completely different. In this case you would fetch all elements that have any of the three values.
– HimBromBeere
Nov 13 '18 at 9:21