2

In addition to my previous question, I still have no concrete solution to my problem.

I have 2 classes like this:

public class Product
{
    public Product()
    {
        Options = new List<Option>();
    }
    public string Name { get; set; }
    public int ID { get; set; }
    public List<Option> Options { get; set; }
}

public class Option
{
    public int SubID { get; set; }
    public string SubName { get; set; }
    public int ListBoxID { get; set; }
    public decimal Price { get; set; }
    public bool IsSelected { get; set; }
}

Which are used in a list like this:

List<Product> Products { get; set; }

enter image description here

When a user selects a product on the left (which are always with the same properties), it's internal List<Optie> will get loaded in 4 different ListBox's, Filtered by ListBoxID.

Now, if a user selects the same options in 2 products, how can I convert them into one result using linq?

e.g. 4 identical products are loaded, user selects the same options for 2 products, then my result would be:

2x Product 2x lookbrood 2x ollema 2x fitness broodje 2x testtest

If the 2 other products have different selections, these might be the result for them:

2x Product 2x ciabatta broodje 2x ollema 2x slagroom 2x smos

I always need the quantity loaded into Products: e.g.: 6 products are loaded, then my combinations can be:

3x Product 3x ciabatta broodje 3x ollema 3x slagroom 3x smos

3x Product 3x Some other option 1 3x Some other option 2 3x Some other option 3 3x Some other option 4

or

2x Product 2x Some option 1 2x Some option 2 2x Some option 3 2x Some option 4

2x Product 2x Some other option 1 2x Some other option 2 2x Some other option 3 2x Some other option 4

2x Product 2x Some other option 1 2x Some other option 2 2x Some other option 3 2x Some other option 4

This is the best explanation I can give, I hope the image helps.

Community
  • 1
  • 1
DeMama
  • 1,134
  • 3
  • 13
  • 32
  • It might be clearer if you could give an example of 2 `Product` with their `Optie` (options?) and what the result should look like if those 2 `Product` are selected. What should be the type of the final result? Should it be a single `Product`? A `List`? How are the `Optie` supposed to be combined? – Matt Burland Oct 27 '14 at 14:06
  • 1
    Argh... why do you mix languages like that? You have a property called IsSelected (nice, English name)... and then a bunch of non-english properties (including the class name). If you ever get a coder from another country your code will be hell to work with for him / her. Other than that, I can't quite make out what you're trying to do. :| – MBender Oct 27 '14 at 14:06
  • @MattBurland Updated my question, it's a complicated thing, I know.. – DeMama Oct 27 '14 at 14:20
  • @Shaamaan That's why I develop on my own ;-), but I updated the property names for you – DeMama Oct 27 '14 at 14:23

1 Answers1

2

Are you looking for GroupBy?. This will group Opties with the same ID, and give you the number of them.

Product.Opties
    .GroupBy(x => new {
        x.ListBoxID, x.Naam
    })
    .Select(x => 
    new { 
        ListBoxID = x.Key.ListBoxID,
        Naam = x.Key.Naam,
        Count = x.Count()
    })

More info on GroupBy

Community
  • 1
  • 1
DLeh
  • 23,806
  • 16
  • 84
  • 128