0

Greetings Everyone
I have model view that contains my method collections that I init in the main Controller but when i try to get an X element without using a foreach it doesn't work, I tried this in the view

@Html.Display(Model.Intel.where(x => x.Item2.Equals("Test"))

but nothing shows. If anyone have an idea i am open to it.
Intel is type of Collection<Tuple<string, string>>

Sleepy
  • 180
  • 3
  • 15

2 Answers2

0

If Model.Intel is a collection, and you want a single string item to pass to @Html.Display, you'll need something to return a string.

Model.Intel.Where([predicate]) 

returns IQueryable

Try something like this to get the item, then address a property or go ToString() on it:

Model.Intel.Where(x => x.Item2.Equals("Test")).Single()

OR

Model.Intel.Where(x => x.Item2.Equals("Test")).Single().StringPropertyOfSingleObject
Colorado Matt
  • 349
  • 3
  • 8
0

you dont really need to use Display.. unless you want to create a Display Template to show the values.. just add the value to your view

@Model.Intel.Where(a => a.Item2 == "Test")
      .Select(a => string.Format("({0}, {1})",a.Item1, a.Item2))
      .Aggregate((i, j) => i + ", " + j)

assuming you might have two items with Item2 = "Test", this would output something like

(1 - Test), (2 - Test)

you can also change the string.Format to wrap the output in html tags.. just make sure you use @Html.Raw() to display it.

<ul>
    @Html.Raw(Model.Intel.Where(a => a.Item2 == "Test")
                   .Select(a => string.Format("<li>{0} - {1}</li>",a.Item1, a.Item2))
                   .Aggregate((i, j) => i + "" + j))
</ul>

would output

  • 1 - Test
  • 2 - Test
JamieD77
  • 13,796
  • 1
  • 17
  • 27
  • @JameD77 it worked but i don't understand what aggregate does & if the i, j part is needed because it's the only way ti worked for me with few tests i made, WHY? because i sometimes need to show 1 item not both of the tuple items – Sleepy Aug 10 '15 at 21:50
  • here's a good explanation of aggregate.. http://stackoverflow.com/questions/7105505/linq-aggregate-algorithm-explained. `i` and `j` represent the aggregate value and the next value in the list. the list itself is formed in the `Select`. This is where you determine if you want Item1 and Item2 in the list or just one or the other – JamieD77 Aug 11 '15 at 13:56
  • If you just want to display Item1 of the tuple you would replace the Select with `.Select(a => a.Item1)` this would give you a `List` since the tuple is `` – JamieD77 Aug 11 '15 at 14:02