I'm trying to do a search for a SKU that the user will enter in a box but i'm a LINQ noob so some help and guidance would be appreciated.
//search
IQueryable<Product> res = from prod in Product.All()
where
(prod.Sku.Where(
prodSku =>
prodSku.ToString() == searchString
).Single())
select prod;
This is my code currently but i'm unsure of the lambda syntax and i'm not sure why it thinks the sku is a char rather than a string.
I must say, this is not enough information to help you.
What I'd like to know is: What is prod.Sku? Is it a collection of strings or just a single string property of "Product"?
You're statement is at least wrong at one point; the .Single() statement. Should be something like:
//search IQueryable<Product> res = from prod in Product.All() where prod.Sku.Where( prodSku => prodSku.ToString() == searchString ).FirstOrDefault() != null) select prod;
Im not sure what else i can say about that bit of code to be honest. I have copied the code from the simple search for umbraco project and tried to make it my own, Like i said, i'm a LINQ noobie so i'm not really sure what it should or shouldn't be saying. this is also the first time i've used the lambda syntax as well.
I am trying to pass a string in to search the SKU field and this was my best attempt at it.
I have tried the code you suggested and i get the following error.
Server Error in '/' Application.
System.String ToString()
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
Sorry if that's not all that helpful but i'm not sure what i am doing really.
var product = Product.All().Where(x => x.Sku == mySku).FirstOrDefault();
This grabs any product with the exact SKU. If you're working with variants it will actually retrieve multiple products so you can narrow it down further if you just want the product family (the top most level of the product).
Bonus info: The ToString call causes our data access layer to fail as it tries to find a SQL equivalent to ToString() which obviously doesn't exist. Generally avoid calling methods in database queries.
If you do need to call methods like IsVariant or something else you can force the database query to be executed by doing a ToList() on the query. Once that's done a different LINQ provider takes over which understands .NET methods.
var products = Product.All().Where(...).ToList(); // forces the query to be executed in the database procuct.Where(x => x.Sku.ToString() == "something"); // you can use any .NET method at this point as LINQ to objects has taken over processing the query
LINQ search for SKU
Hi,
I'm trying to do a search for a SKU that the user will enter in a box but i'm a LINQ noob so some help and guidance would be appreciated.
This is my code currently but i'm unsure of the lambda syntax and i'm not sure why it thinks the sku is a char rather than a string.
Any ideas would be appreciated.
Thanks
Chris
I must say, this is not enough information to help you.
What I'd like to know is: What is prod.Sku? Is it a collection of strings or just a single string property of "Product"?
You're statement is at least wrong at one point; the .Single() statement. Should be something like:
Im not sure what else i can say about that bit of code to be honest. I have copied the code from the simple search for umbraco project and tried to make it my own, Like i said, i'm a LINQ noobie so i'm not really sure what it should or shouldn't be saying. this is also the first time i've used the lambda syntax as well.
I am trying to pass a string in to search the SKU field and this was my best attempt at it.
I have tried the code you suggested and i get the following error.
Server Error in '/' Application.
System.String ToString()
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: System.String ToString()
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
Sorry if that's not all that helpful but i'm not sure what i am doing really.
Thanks
Chris
You'll want something along the lines of
This grabs any product with the exact SKU. If you're working with variants it will actually retrieve multiple products so you can narrow it down further if you just want the product family (the top most level of the product).
Hope this helps.
Bonus info: The ToString call causes our data access layer to fail as it tries to find a SQL equivalent to ToString() which obviously doesn't exist. Generally avoid calling methods in database queries.
If you do need to call methods like IsVariant or something else you can force the database query to be executed by doing a ToList() on the query. Once that's done a different LINQ provider takes over which understands .NET methods.
Thank you so much Soren!
Working a treat!
Wonderful! :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.