Linq where expressions as values to add












0














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









share|improve this question




















  • 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








  • 1




    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






  • 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
















0














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









share|improve this question




















  • 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








  • 1




    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






  • 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














0












0








0







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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 declare qx outside 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 OR condition
    – 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




    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




    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






  • 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












4 Answers
4






active

oldest

votes


















0














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.






share|improve this answer





























    0














    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";





    share|improve this answer





























      0














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





      share|improve this answer





















      • 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










      • I totally agree. That's why I don't use it often.
        – Tsahi Asher
        Nov 13 '18 at 13:40



















      -1














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





      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        0














        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.






        share|improve this answer


























          0














          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.






          share|improve this answer
























            0












            0








            0






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 9:12









            Tsahi AsherTsahi Asher

            1,133822




            1,133822

























                0














                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";





                share|improve this answer


























                  0














                  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";





                  share|improve this answer
























                    0












                    0








                    0






                    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";





                    share|improve this answer












                    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";






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 9:23









                    mshwfmshwf

                    2,13742049




                    2,13742049























                        0














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





                        share|improve this answer





















                        • 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










                        • I totally agree. That's why I don't use it often.
                          – Tsahi Asher
                          Nov 13 '18 at 13:40
















                        0














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





                        share|improve this answer





















                        • 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










                        • I totally agree. That's why I don't use it often.
                          – Tsahi Asher
                          Nov 13 '18 at 13:40














                        0












                        0








                        0






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





                        share|improve this answer












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






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 13 '18 at 10:18









                        Daniel LoudonDaniel Loudon

                        678116




                        678116












                        • 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










                        • 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










                        • 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
















                        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











                        -1














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





                        share|improve this answer




























                          -1














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





                          share|improve this answer


























                            -1












                            -1








                            -1






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





                            share|improve this answer














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






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 9:19

























                            answered Nov 13 '18 at 9:14









                            HimBromBeereHimBromBeere

                            23.1k43159




                            23.1k43159






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                List item for chat from Array inside array React Native

                                Thiostrepton

                                Caerphilly