0

I have this code which, on the front-end, will create dependent selectboxes (subcategories are dependent on the category) using LINQ:

foreach (var cat in (from category in KB.Categories
                     orderby category.name
                     select category)) {
    this.categories.Add(cat.id, cat.name);
}
foreach (var sub_cat in (from subcategory in KB.SubCategories
                      orderby subcategory.name
                      select subcategory)) {
    this.subcategories.Add(sub_cat.id, sub_cat.name);
    if (!this.subcategoryCategory.containsKey) {
        this.subcategoryCategory.Add(sub_cat.category_id, new ArrayList());
    }
    // I'd like to put the sub_cat_id at the end of the arraylist
    // for the category_id, but this line doesn't seem to work
    //this.subcategoryCategory[sub_cat.category_id] = sub_cat.id;
}

How can I do this?

Perhaps there a way to build a giant JSON object instead of the three variables (categories, subCategoryCategory, subcategories)?

Is there a better/different way to do this that I've completely missed?

P.S. Coming from a different programming paradigm, I'm not doing this in the standard ASP.NET (webforms or MVC) way, but I am using codebehind to generate the values.

Damiro
  • 251
  • 4
  • 5
  • 14
  • Given that you're using LINQ and therefore *must* have generics available, why are you using the old collections instead of `List` etc? – Jon Skeet Apr 04 '11 at 15:02
  • Because I'm new at this (ASP.NET) and have yet to wrap my head around the whole 'creating a class which defines a variable type' when I can use a generic Hashtable. :) Thanks for taking the time to help. – Damiro Apr 04 '11 at 15:16
  • This isn't really an ASP.NET issue. It's "core" .NET. I would strongly suggest you try to get your head round generics before you do any more LINQ work. – Jon Skeet Apr 04 '11 at 15:20

2 Answers2

0

It looks like you actually want a Lookup, e.g.

var subcategories = KB.SubCategories.ToLookup(subcat => subcat.id,
                                              subcat => subcat.name);

However, it's not really clear given that you've got subcategories, subcategoryCategory and categoies, all of which are instance variables which you haven't shown us the type for... and your if clause doesn't specify which key it's trying to use. It's all a bit confused at the moment...

My guess is that you should look at ToLookup and also ToDictionary, which are made for this sort of thing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • They are all `subcategories`, `subCategoryCategory`, and `categories` are all Hashtables. The key I'm looking for should be `sub_cat.id`. – Damiro Apr 04 '11 at 15:08
  • @Damiro: This is where we're suffering because you're not using the generic collections - it's hard for us to know what you expect when you're using weakly typed collections. Why would you want to do that? – Jon Skeet Apr 04 '11 at 15:11
  • Thanks, I'll look into these. It seems to make the code more clear. The main issue, however is accessing the `ArrayList` within the subCategoryCategory Hashtable. Any help is appreciated. – Damiro Apr 04 '11 at 15:12
  • @Damiro: That's only the "main issue" because you're looking at it from the point of view of a decision you've already made - which I don't think is the right decision. If you use ToLookup you'll probably find it's a single statement to do the right thing completely. But we can't easily tell that at the moment. – Jon Skeet Apr 04 '11 at 15:16
  • So the lookup could create some sort of mapping something along the lines of `{'a':(1,2,3),'b':(4,5,6)}`? My _real_ requirement is that of being able to easily loop through `a` and `b` to output the related numbers `(1,2,3,4,5,6)`. – Damiro Apr 04 '11 at 15:21
  • @Damiro: Yes. All of that would be very easy. – Jon Skeet Apr 04 '11 at 15:29
0

JSON.NET might solve this problem, with its ability to convert JSON objects to C# and back. See this SO question for an example.

Community
  • 1
  • 1
neontapir
  • 4,698
  • 3
  • 37
  • 52