0

I am very new to C#, but I am in need of changing a small function that looks at an array. In the code I am working on, foreach is used to go through the array of items and render them on a webpage as list items. For now, I have to find various blocks of code like this and change them to not loop through whole arrays, but pick specific items and just render them.

If I just want to pull out the absolute newest item in the array, how would I do that? Example of what I am in need of changing: #foreach($product in $Website.Products) needs to be changed to something like #firstitem($product in $Website.Products)

Here is the whole block for context:

    <div class="slider-content">
        #if($Website.Products.Count != 0)
        <ul class="slider-list">
            #foreach($product in $Website.Products)
            <li class="slider-page">
                <div class="vdd-container">
                    <div class="vdd">
                        <blockquote>
                            <span class="quote-open"></span>
                            <q><span>${product.Message}</span></q>
                            <span class="quote-close"></span>
                        </blockquote>
                    </div>
                </div>
                <cite>
                    <strong class="pnx">${product.Name}</strong>
                </cite>
            </li>
            #end
        </ul>
        #else
        <div class="not-found">No products in store.</div>
        #end
    </div>

Again, just in need of outputting the first item instead of looping through and doing each.

Thanks.

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
user1729506
  • 975
  • 4
  • 15
  • 28

2 Answers2

2

Take a look at the LINQ First() and/or FirstOrDefault() extension methods. They allow you to get the first item in any IEnumerable<T>. You can also specify a condition that must be met

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.first.aspx

//Gets the first product in the Products collection
var firstProduct = Website.Products.First();

//Gets the first product where a given condition is true
var firstExpensiveProduct = Website.Products.First(p => p.Cost > 100);
cordialgerm
  • 8,403
  • 5
  • 31
  • 47
1

You should be able to do this in a number of ways.

  1. You can use an array accessor: $Website.Products[0]
  2. You can use LINQ: $Website.Products.First()

The first option is more efficient when working with simple arrays. The latter option may look better in some circumstances, and may perform better if you are using certain types of collections (instead of a simple array).


Your template syntax implies that you are using the nVelocity template engine. As mentioned in other SO questions, nVelocity does not seem to be able to handle Extension Methods. Since First() is an extension method, that means you can't use it.

The array accessor should work though.

Community
  • 1
  • 1
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
  • I tried `$Website.Products.First()` and got an error. I worry I am messing up the syntax. Later in the code, since they are looping through this array and assigning everything to $product, there is a bit that reads `${product.Message}` Since I am forgoing the array to just call the first item, would I put `${Website.Products.First().Message}` ? – user1729506 Dec 13 '12 at 02:35
  • @user1729506 it would be helpful if you could post the error that you get. – Moshe Katz Dec 13 '12 at 02:55
  • Just not getting the output desired. An error is occurring, but no active debug-info dumper to which I have access can give me explicit data to show you. Apologies. – user1729506 Dec 13 '12 at 02:58
  • If you wanted to just reference the `Message` property, yes you would do exactly that. However, since you said that `First()` doesn't work, have you tried using the array accessor (`[0]`) instead? – Moshe Katz Dec 13 '12 at 03:01
  • Is `${Website.Products.First().Message}` valid syntax, though? It literally outputs this on the page-is. It doesn't replace it with the array data. – user1729506 Dec 13 '12 at 03:08
  • If it doesn't replace it, that means it can't find a matching method on the object, so it assumes that you mean that as plain text. My guess would be that it's because `First` is an Extension Method. Other SO questions imply that nVelocity has difficulty calling Extension Methods. See the update to my answer. – Moshe Katz Dec 13 '12 at 03:12