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.

  • Slava 9 posts 29 karma points
    Nov 17, 2010 @ 17:48
    Slava
    0

    How to get products in specified categories

    Hi,

    I need to find all products in specified categories. For example, user enters "woman, shoes, adidas" in filtering textbox, then I need to find all woman shoes from Adidas, so that get a list of products where each product included in all categories user wrote (logical AND).

    I started with some linq query that didn't work, then after changes I still was getting different kinds of errors.  Seems like NHibernate linq provider doesn't support methods (e.g.string.IsNullOrEmpty) in lambda expression, but more important it fails with complex queries... I ended up to call .AsIEnumerable() on sub-queries to force getting data from sql server in a simple way and moved other processing on web server side...

    Please take a look at one of current query below which is of course not efficeint, but doesn't generate exception at least. But it gives me zero results while I know there are a lot of products in categories I am testing.

    IEnumerable<int> categoriesIds = Category.All().AsEnumerable<Category>()
                    .Where<Category>((c => categoriesNames.Contains(c.Name.Trim().ToLower())))
                    .Select<Category, int>(c => c.CategoryId);

    var filteredProducts =    from p in Product.All().AsEnumerable<Product>()
                                       let productCategories = p.CategoryProductRelations.Select<CategoryProductRelation, int>(cpr => cpr.CategoryId)
                                       where /*p.ParentProductId == null && (p.VariantSku == null || p.VariantSku == "")
                                           &&*/ productCategories.OrderBy(id => id).SequenceEqual(categoriesIds.OrderBy(id => id))
                                       orderby p.Name ascending
                                       select p;

    Could someone please help me this issue? Maybe I missing soemthing, and I am using API in wrong way?

    Maybe it is EntitiesV2 issue, and with subsonic version it is possieblt to compose needed query?

    Thanks in advcance.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Nov 22, 2010 @ 13:00
    Søren Spelling Lund
    0

    Hi Slava,

    Your question inspired me to do a post on how to do put together the query. I've reposted the contents here for your convenience:

    // These are the categories our products must be present in
    string
    [] categoryNames = { "Software", "Support" };
    var query = from product in Product.All()
    where product.CategoryProductRelations.Where(x => categoryNames.Contains(x.Category.Name)).Count() == categoryNames.Count()
    select product;

    It basically looks for the categories specified in the categoryNames array and for each product counts whether the number matched is the same as the number we're looking for.

     

  • Slava 9 posts 29 karma points
    Nov 22, 2010 @ 16:43
    Slava
    0

    Hi Søren,

    Thank you. Your proposed query has helped me.

    And I would like to mention couple of things just to share some of my exprerinces.

    First, if categoryNames is for example IEnumerable instead of array, then it doesn't work as expected. Second, is that when I added next additional filtering to where clause

    p.ParentProductId == null && (p.VariantSku == null || p.VariantSku == "")

    then code doesn't fail, but works incorrectly  - doesn't filter products.

    So it is just remind that Linq to Something is not so always so sweet like Linq to Objects :). Therefore, developer have to sort out things and find out limitations of  particular linq provider.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Nov 22, 2010 @ 20:26
    Søren Spelling Lund
    0

    Which version of uCommerce are you running?

  • Slava 9 posts 29 karma points
    Nov 23, 2010 @ 09:35
    Slava
    0

    It is 1.1.1.0 version.

     

  • Søren Spelling Lund 1797 posts 2786 karma points
    Nov 23, 2010 @ 09:45
    Søren Spelling Lund
    0

    Version 1.2 will include  upgraded LINQ provider. If you're interested I can send you a prerelease version to play with if you want to try it out. Shoot me an e-mail at [email protected]

    We've been using the new provider internally for a while now.

  • Slava 9 posts 29 karma points
    Nov 23, 2010 @ 12:20
    Slava
    0

    Thanks, but I managed to get needed functionality working for now, so it is ok as it is now.

  • Tony Kiernan 278 posts 341 karma points
    Oct 25, 2011 @ 11:53
    Tony Kiernan
    0

    So, is there a more uCommerce-y way of doing this with 2.0?

  • Tony Kiernan 278 posts 341 karma points
    Oct 25, 2011 @ 12:59
    Tony Kiernan
    0

    I'm using this (added to a list to work with my paging functions):

    var myCategories = Category.All().Where(x => x.DisplayOnSite && (x.CategoryId == int.Parse(myCategoryID) || x.ParentCategoryId == int.Parse(myCategoryID)));
            foreach (var myCategory in myCategories)
            {
                myProductsAll = Product.GetForCategory(cat).Where(x => x.ParentProductId == null && x.DisplayOnSite && x.AllowOrdering);
                productList.AddRange(myProductsAll.ToList());
            }

    I'm sure that loop should not be necessary

  • Søren Spelling Lund 1797 posts 2786 karma points
    Nov 08, 2011 @ 11:32
    Søren Spelling Lund
    0

    You could do a join to avoid the loop.

  • Tony Kiernan 278 posts 341 karma points
    Nov 08, 2011 @ 12:51
    Tony Kiernan
    0

    Thankfully, the client has changed their mind, but that is how I would do it now.

Please Sign in or register to post replies

Write your reply to:

Draft