“LINQ to Entities does not recognize the method” without using local












0















I want to query count of selected products with a Combobox in VB.NET, Linq and Entity Framework 6. This query generates error (cmbProducts is a Combobox):



    Dim Count = (From Product In db.Products
Where Product.Type = cmbProducts.SelectedValue
).Count


And this is the error:



LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.


But when I run this query with db.Products.local, it executes without any errors:



    Dim Count = (From Product In db.Products.local
Where Product.Type = cmbProducts.SelectedValue
).Count









share|improve this question



























    0















    I want to query count of selected products with a Combobox in VB.NET, Linq and Entity Framework 6. This query generates error (cmbProducts is a Combobox):



        Dim Count = (From Product In db.Products
    Where Product.Type = cmbProducts.SelectedValue
    ).Count


    And this is the error:



    LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.


    But when I run this query with db.Products.local, it executes without any errors:



        Dim Count = (From Product In db.Products.local
    Where Product.Type = cmbProducts.SelectedValue
    ).Count









    share|improve this question

























      0












      0








      0








      I want to query count of selected products with a Combobox in VB.NET, Linq and Entity Framework 6. This query generates error (cmbProducts is a Combobox):



          Dim Count = (From Product In db.Products
      Where Product.Type = cmbProducts.SelectedValue
      ).Count


      And this is the error:



      LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.


      But when I run this query with db.Products.local, it executes without any errors:



          Dim Count = (From Product In db.Products.local
      Where Product.Type = cmbProducts.SelectedValue
      ).Count









      share|improve this question














      I want to query count of selected products with a Combobox in VB.NET, Linq and Entity Framework 6. This query generates error (cmbProducts is a Combobox):



          Dim Count = (From Product In db.Products
      Where Product.Type = cmbProducts.SelectedValue
      ).Count


      And this is the error:



      LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.


      But when I run this query with db.Products.local, it executes without any errors:



          Dim Count = (From Product In db.Products.local
      Where Product.Type = cmbProducts.SelectedValue
      ).Count






      vb.net linq-to-sql entity-framework-6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 4:59









      rostamianirostamiani

      3911527




      3911527
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You really should turn Option Strict On in the project properties and also in the IDE options, so it will be On by default for all future projects. If you do that then that code won't even compile. That would force you to do as you should have and cast the SelectedValue, which is type Object, as the actual type of the underlying object, which is presumably String or Integer. You can use DirectCast or else CInt, CStr or the like to perform the cast, e.g.



          Where Product.Type = CInt(cmbProducts.SelectedValue)


          Ideally, you should be assigning the result of that cast to a variable and using that in your LINQ query. It's important to remember that, while LINQ syntax is always the same, each LINQ provider has a different implementation. LINQ to Entities converts your query to SQL that it can execute against the database and that means that some things that are supported by LINQ in general, and will thus compile, will actually fail at run-time against LINQ to Entities. It's generally a good idea to keep anything remotely exotic out of your EF queries. You'd probably be OK in this case but it's a good habit to get into as it can help avoid subtle issues.






          share|improve this answer

































            0














            Make sure they are of the same type.



            I think Product.Type is a string but cmbProducts.SelectedValue is an int, try to use cmbProducts.SelectedItem.Text



            When comparing locally .Net will be able to compare them by SQL Server may not.






            share|improve this answer
























            • Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

              – rostamiani
              Nov 14 '18 at 6:39













            • That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

              – jmcilhinney
              Nov 14 '18 at 6:46











            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%2f53293431%2flinq-to-entities-does-not-recognize-the-method-without-using-local%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









            1














            You really should turn Option Strict On in the project properties and also in the IDE options, so it will be On by default for all future projects. If you do that then that code won't even compile. That would force you to do as you should have and cast the SelectedValue, which is type Object, as the actual type of the underlying object, which is presumably String or Integer. You can use DirectCast or else CInt, CStr or the like to perform the cast, e.g.



            Where Product.Type = CInt(cmbProducts.SelectedValue)


            Ideally, you should be assigning the result of that cast to a variable and using that in your LINQ query. It's important to remember that, while LINQ syntax is always the same, each LINQ provider has a different implementation. LINQ to Entities converts your query to SQL that it can execute against the database and that means that some things that are supported by LINQ in general, and will thus compile, will actually fail at run-time against LINQ to Entities. It's generally a good idea to keep anything remotely exotic out of your EF queries. You'd probably be OK in this case but it's a good habit to get into as it can help avoid subtle issues.






            share|improve this answer






























              1














              You really should turn Option Strict On in the project properties and also in the IDE options, so it will be On by default for all future projects. If you do that then that code won't even compile. That would force you to do as you should have and cast the SelectedValue, which is type Object, as the actual type of the underlying object, which is presumably String or Integer. You can use DirectCast or else CInt, CStr or the like to perform the cast, e.g.



              Where Product.Type = CInt(cmbProducts.SelectedValue)


              Ideally, you should be assigning the result of that cast to a variable and using that in your LINQ query. It's important to remember that, while LINQ syntax is always the same, each LINQ provider has a different implementation. LINQ to Entities converts your query to SQL that it can execute against the database and that means that some things that are supported by LINQ in general, and will thus compile, will actually fail at run-time against LINQ to Entities. It's generally a good idea to keep anything remotely exotic out of your EF queries. You'd probably be OK in this case but it's a good habit to get into as it can help avoid subtle issues.






              share|improve this answer




























                1












                1








                1







                You really should turn Option Strict On in the project properties and also in the IDE options, so it will be On by default for all future projects. If you do that then that code won't even compile. That would force you to do as you should have and cast the SelectedValue, which is type Object, as the actual type of the underlying object, which is presumably String or Integer. You can use DirectCast or else CInt, CStr or the like to perform the cast, e.g.



                Where Product.Type = CInt(cmbProducts.SelectedValue)


                Ideally, you should be assigning the result of that cast to a variable and using that in your LINQ query. It's important to remember that, while LINQ syntax is always the same, each LINQ provider has a different implementation. LINQ to Entities converts your query to SQL that it can execute against the database and that means that some things that are supported by LINQ in general, and will thus compile, will actually fail at run-time against LINQ to Entities. It's generally a good idea to keep anything remotely exotic out of your EF queries. You'd probably be OK in this case but it's a good habit to get into as it can help avoid subtle issues.






                share|improve this answer















                You really should turn Option Strict On in the project properties and also in the IDE options, so it will be On by default for all future projects. If you do that then that code won't even compile. That would force you to do as you should have and cast the SelectedValue, which is type Object, as the actual type of the underlying object, which is presumably String or Integer. You can use DirectCast or else CInt, CStr or the like to perform the cast, e.g.



                Where Product.Type = CInt(cmbProducts.SelectedValue)


                Ideally, you should be assigning the result of that cast to a variable and using that in your LINQ query. It's important to remember that, while LINQ syntax is always the same, each LINQ provider has a different implementation. LINQ to Entities converts your query to SQL that it can execute against the database and that means that some things that are supported by LINQ in general, and will thus compile, will actually fail at run-time against LINQ to Entities. It's generally a good idea to keep anything remotely exotic out of your EF queries. You'd probably be OK in this case but it's a good habit to get into as it can help avoid subtle issues.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 6:53

























                answered Nov 14 '18 at 6:42









                jmcilhinneyjmcilhinney

                25.6k22032




                25.6k22032

























                    0














                    Make sure they are of the same type.



                    I think Product.Type is a string but cmbProducts.SelectedValue is an int, try to use cmbProducts.SelectedItem.Text



                    When comparing locally .Net will be able to compare them by SQL Server may not.






                    share|improve this answer
























                    • Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                      – rostamiani
                      Nov 14 '18 at 6:39













                    • That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                      – jmcilhinney
                      Nov 14 '18 at 6:46
















                    0














                    Make sure they are of the same type.



                    I think Product.Type is a string but cmbProducts.SelectedValue is an int, try to use cmbProducts.SelectedItem.Text



                    When comparing locally .Net will be able to compare them by SQL Server may not.






                    share|improve this answer
























                    • Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                      – rostamiani
                      Nov 14 '18 at 6:39













                    • That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                      – jmcilhinney
                      Nov 14 '18 at 6:46














                    0












                    0








                    0







                    Make sure they are of the same type.



                    I think Product.Type is a string but cmbProducts.SelectedValue is an int, try to use cmbProducts.SelectedItem.Text



                    When comparing locally .Net will be able to compare them by SQL Server may not.






                    share|improve this answer













                    Make sure they are of the same type.



                    I think Product.Type is a string but cmbProducts.SelectedValue is an int, try to use cmbProducts.SelectedItem.Text



                    When comparing locally .Net will be able to compare them by SQL Server may not.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 5:32









                    bestinamirbestinamir

                    5917




                    5917













                    • Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                      – rostamiani
                      Nov 14 '18 at 6:39













                    • That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                      – jmcilhinney
                      Nov 14 '18 at 6:46



















                    • Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                      – rostamiani
                      Nov 14 '18 at 6:39













                    • That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                      – jmcilhinney
                      Nov 14 '18 at 6:46

















                    Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                    – rostamiani
                    Nov 14 '18 at 6:39







                    Thanks.But they are of the same type. SelectedIndex is int but SelectedValue can have various types.

                    – rostamiani
                    Nov 14 '18 at 6:39















                    That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                    – jmcilhinney
                    Nov 14 '18 at 6:46





                    That's not really correct. It is a typing issue but not the way you describe. The issue is that Option Strict is Off and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue and Option Strict On will enforce that cast. Also, there's possibly no such thing as cmbProducts.SelectedItem.Text, which would rely on late-binding anyway and may result in a missing member exception.

                    – jmcilhinney
                    Nov 14 '18 at 6:46


















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293431%2flinq-to-entities-does-not-recognize-the-method-without-using-local%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python