Search code examples
c#linqrepeater

Nested Repeater Distinct on the Parent


I look through a bunch of examples and I am stuck on this issue....

Data

CategoryId  OptionId    CategoryName    OptionName
    1          2           Type            Black & White
    1          3           Type            Color
    2          4           Print Speed     Less than 21 ppm
    2          5           Print Speed     21-30 ppm
    2          6           Print Speed     31-45 ppm
    2          7           Print Speed     46-55 ppm
    2          8           Print Speed     56-70 ppm
    2          10          Print Speed     71 plus ppm
    3          11          Software        3.5
    3          12          Software        4
    4          13          User Interface  7" Touch-Screen
    4          14          User Interface  8.5" Touch-Screen
    4          15          User Interface  10.1" Touch-Screen
    4          16          User Interface  15.4" Touch-Screen

I want the parent repeater to do distinct on CategoryName but when I do that I don't get all the OptionNames within my nested repeator. I am using a dataTable to populate the parent repeater. I have tried linq and distinct which will show the categoryname correctly and the options but I Need the ID's in the nested repeater...

I am using this example - Databind repeater using Linq with group by how do I change the linq to include more fields, I am not very familiar with Linq.

Parent Repeater Databind
    Repeater1.DataSource = (from x in csvData.AsEnumerable() select x["category"]).Distinct();

Nested Repeater Databind
    rptr.DataSource = csvData.AsEnumerable().Where(x => x["category"].Equals(e.Item.DataItem));

Solution

  • var output = catOptData.AsEnumerable()
                                .Select(x => new { CategoryId = x["CategoryId"], CategoryName = x["CategoryName"] })
                                .Distinct();
    

    Did that for the parent and for nested I did

    rptr.DataSource = catOptData.AsEnumerable().Where(x => x["CategoryName"].Equals(DataBinder.Eval(e.Item.DataItem, "CategoryName").ToString())).AsDataView();