0

I am creating a tree view for a flat table. I have a linq query which returns all distinct values from a column

        public ActionResult Index()
    {
        var result = (from i in db.Items
                     select i.GeoName)
                     .Distinct()
                     .OrderBy(n => n);

        return View(result);
    }

This of course returns a view with my beginning nodes (lets hypothetically say 'Node1', 'Node2', and 'Node3').

I would like to then have the user click on a node, and return the contents of that column as well.

How do you do this dynamically?

I was hoping it was as simple (and may well be, I don't know - LINQ newb!) as:

        public ActionResult Index()
    {
        var result = (from i in db.Items
                     select i.passedInColumnName)
                     .Distinct()
                     .OrderBy(n => n);

        return View(result);
    }

As always, thanks in advance to you helpful folks.

mrwienerdog
  • 815
  • 3
  • 18
  • 35
  • 1
    look into Dynamic LINQ – Selman Genç Sep 10 '14 at 14:32
  • Surely you always want to return the same columns (i.e. the `select`) but want to limit the range being returned (i.e. apply a `where` clause)? – DavidG Sep 10 '14 at 14:36
  • an easier way might be to load all the columns you want in the initial view and show or hide the columns based on the node click rather that calling Index everytime – theDarse Sep 10 '14 at 14:46
  • Unfortunately, it's a huge table. Takes a very long time to load all fields, I'm loading via ajax depending on the node clicked. – mrwienerdog Sep 10 '14 at 14:47
  • @theDarse - I am actually not returning the same column (in the first node, I am displaying the distinct values contained within the column named 'GeoNode'). Then, depending on which node is selected, I then want to display all distinct values contained within the column corresponding to that node. – mrwienerdog Sep 10 '14 at 14:49
  • You are looking for a way to do dynamic selects with LINQ. This has already been discussed here: http://stackoverflow.com/questions/16516971/linq-dynamic-select – Postlagerkarte Sep 10 '14 at 14:51

1 Answers1

1

You can do this using Expression Trees but its pretty in depth, I would suggest using a SQL as Linq isn't really the best solution for what you want to do.

the SQL for this would be:

SELECT DISTINCT
    passedInColumnName
FROM
    Items
WHERE
    GeoName = 'the geo name'
ORDER BY
    passedInColumnName
Antony Jones
  • 561
  • 1
  • 4
  • 15