Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Chris Knowles 141 posts 222 karma points
    Dec 05, 2011 @ 18:15
    Chris Knowles
    0

    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.

                //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.

    Any ideas would be appreciated.

    Thanks

    Chris

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 05, 2011 @ 18:56
    Stefan Kip
    0

    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;
  • Chris Knowles 141 posts 222 karma points
    Dec 06, 2011 @ 12:54
    Chris Knowles
    0

    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

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 08, 2011 @ 20:53
    Søren Spelling Lund
    0

    You'll want something along the lines of

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

    var product = Product.All().Where(x => x.Sku == mySku && x.ParentProductId == null).FirstOrDefault();

    Hope this helps.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 08, 2011 @ 20:57
    Søren Spelling Lund
    0

    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
  • Chris Knowles 141 posts 222 karma points
    Dec 09, 2011 @ 10:49
    Chris Knowles
    0

    Thank you so much Soren! 

    Working a treat!

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 13, 2011 @ 13:15
    Søren Spelling Lund
    0

    Wonderful! :)

Please Sign in or register to post replies

Write your reply to:

Draft