“LINQ to Entities does not recognize the method” without using local
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
add a comment |
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
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
vb.net linq-to-sql entity-framework-6
asked Nov 14 '18 at 4:59
rostamianirostamiani
4301730
4301730
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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 thatOption Strict
isOff
and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue andOption Strict On
will enforce that cast. Also, there's possibly no such thing ascmbProducts.SelectedItem.Text
, which would rely on late-binding anyway and may result in a missing member exception.
– jmcilhinney
Nov 14 '18 at 6:46
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%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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 14 '18 at 6:53
answered Nov 14 '18 at 6:42
jmcilhinneyjmcilhinney
26.5k32033
26.5k32033
add a comment |
add a comment |
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.
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 thatOption Strict
isOff
and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue andOption Strict On
will enforce that cast. Also, there's possibly no such thing ascmbProducts.SelectedItem.Text
, which would rely on late-binding anyway and may result in a missing member exception.
– jmcilhinney
Nov 14 '18 at 6:46
add a comment |
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.
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 thatOption Strict
isOff
and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue andOption Strict On
will enforce that cast. Also, there's possibly no such thing ascmbProducts.SelectedItem.Text
, which would rely on late-binding anyway and may result in a missing member exception.
– jmcilhinney
Nov 14 '18 at 6:46
add a comment |
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.
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.
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 thatOption Strict
isOff
and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue andOption Strict On
will enforce that cast. Also, there's possibly no such thing ascmbProducts.SelectedItem.Text
, which would rely on late-binding anyway and may result in a missing member exception.
– jmcilhinney
Nov 14 '18 at 6:46
add a comment |
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 thatOption Strict
isOff
and the OP is relying on an implicit conversion, which is not supported by LINQ to Entities. A simple cast will fix the issue andOption Strict On
will enforce that cast. Also, there's possibly no such thing ascmbProducts.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
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.
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%2f53293431%2flinq-to-entities-does-not-recognize-the-method-without-using-local%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