locked
Other table data getting lost after filtering object list RRS feed

  • Question

  • User-1374873858 posted

    I have an ecommerce project that has to return products based on category. After I get all products from the database i include their image(which is on another table) with .Include and I try to filter the products based on the category the customer chose. After that images are no longer in the returned list. Why am i getting this behavior?

    this is how i retrieve products from database

    var result = _context.Products
                    .Include(x => x.Categories).ThenInclude(x => x.Category)
                    .Include(x => x.Images).ThenInclude(x => x.Image)
                    .Include(x => x.Manufacturers).ThenInclude(x => x.Manufacturer)
                    .Include(x => x.Specifications).ThenInclude(x => x.Specification)
                    .AsNoTracking();

    this is how i filter them

    if(categoryFilter != null && categoryFilter.Length > 0)
                {
                    result = result.Where(x => x
                        .Categories.Select(c => c.Category.Name.ToLower())
                        .Intersect(categoryFilter.Select(cf => cf.ToLower()))
                        .Count() > 0
                    );
                }

    category filter is string[] list

    Thursday, August 13, 2020 4:16 PM

All replies

  • User-17257777 posted

    Hi Kout,

    The reason you didn't get your picture is because you are using the AsNoTracking method.

    This method can only read the data and cannot perform other operations on the data.

    It may be a good choice to change your AsNoTracking method to Tolist.

    var result = context.Products
    .Include(x => x.Categories).ThenInclude(x => x.Category) .Include(x => x.Images).ThenInclude(x => x.Image)
    .Include(x => x.Manufacturers).ThenInclude(x => x.Manufacturer)
    .Include(x => x.Specifications).ThenInclude(x => x.Specification)
    .ToList(); result = result.Where(x => x .Categories.Select(c => c.Category.Name.ToLower()) .Intersect(categoryFilter.Select(cf => cf.ToLower())) .Count() > 0)
    .ToList();

    Best Regards,

    Jiadong Meng

    Friday, August 14, 2020 7:07 AM
  • User-1374873858 posted

    Hello Jiadong, and thanks for your reply.

    I tried your solution and I still have the problem. I was getting one image before, and now i am getting two(lol) out of 8 products. Is there any possibility that the Where() function alters the data stored in the result variable?

    Thank you in advance, Kout

    Monday, August 17, 2020 9:01 AM
  • User-17257777 posted

    Hi Kout,

    Is there any possibility that the Where() function alters the data stored in the result variable?

    The Where() statement won't change the data, it only filters. Have you made some changes to your database before, you may have a check.

    Best Regards,

    Jiadong Meng

    Tuesday, August 18, 2020 2:00 AM
  • User-1374873858 posted

    Hi Jiadong, 

    The thing is in the first block of code as I retrieve all the products from the database, their images come with them but after I filter them in the second block of code I posted the images are lost (BTW not all of them as I mentioned before). I can't find what am I missing.


    Thank you for the help, best regards 

    Kout

    Tuesday, August 18, 2020 8:42 AM
  • User-17257777 posted

    Hi Kout,

    their images come with them but after I filter them in the second block of code I posted the images are lost (BTW not all of them as I mentioned before).

    Maybe you can show us your data source, and the result you expected after filtering.

    Best Regards,

    Jiadong Meng

    Friday, August 21, 2020 8:46 AM
  • User-1374873858 posted

    By saying datasource you mean my database?? 

    Saturday, August 22, 2020 8:23 AM