views:

61

answers:

3

I'm very new to .Net C# and I really don't have a clue as to what I'm doing wrong. I have a LinqToSql DataContext I'm drawing distinct results from, but I'm not sure how to properly output the results in my view. I'm getting a compilation error (Compiler Error Message: CS1002: ; expected) on my view.

StoreRepository.cs

public IQueryable<String> GetStoreStates()
        {
            return from Store in db.Stores
                   orderby Store.State
                   select Convert.ToString(Store.State.Distinct());
        }

StoresController.cs

public ActionResult StatesIndex()
        {
            var states = repo.GetStoreStates().ToList();
            return View("StatesIndex", states);
        }

StatesIndex.aspx

<ul>
        <% foreach (var state in Model)
           { %>
        <li>
            <% Html.Encode(state) %>
        </li>
        <% } %>
    </ul>
A: 

You need to repo.GetStoreStates().ToList(). Without it you are trying to pass an IQueryable as your model, which is essentially a query that hasn't run yet. To run the query you need to call ToList(), which will cause the query to run and will return a List<type>, which you can then loop through.

KallDrexx
Thanks. That was part of it for sure.
E-Madd
+2  A: 

You are missing an equals sign:

        <% Html.Encode(Store.state) %>

should be

        <% =Html.Encode(Store.state) %>

To provide a little more explanation. If you are calling one of the Html extension methods, you need to prefix it with either an equals sign = or a colon : because these methods output the appropriate HTML string to be displayed. When you do that, you dont append your statement with a semicolon.

If you are calling a method that does not directly return an HTML string, then you call it just like a regular C# method, and, in that case, you will need the semicolon.

Remembering when to use equals and when to use semicolon can tend to trip you up a bit when you're first starting out using MVC.

Robaticus
Thank you! OK, now I'm getting a different error. "Sequence operators not supported for type 'System.String'"
E-Madd
A: 

Your View model is a collection of strings, not store objects. So when you are doing

foreach(var Store in Model) each Store is only a string and you can't do Store.state

Either change your GetStoreStates method to return a list of store object or change the contents of your foreach to

<%= Html.Encode(Store) %>

Edit: Updated after comments.

The problem is that you are trying to execute Distinct() on a string. If it had worked it would only have gotten you a string with distinct characters in it.

It think this is more what you want:

public IQueryable<String> GetStoreStates()
{
    return (from Store in db.Stores
           orderby Store.State
           select Store.State).Distinct();
}

This will execute Distinct() on a list of states instead of on each state string.

Jesper Palm
Thanks, yeah I caught that after I posted the question. Though, I am now getting the error "Sequence operators not supported for type 'System.String'"
E-Madd
Can you edit your question and show the new code? Keep the old stuff there, too, please. It would also help to know what type of model your page is expecting.
Robaticus
I've updated the code. The controller now uses the .ToList() and the view is updated to not expect a Store object with a state property. What I'm trying to accomplish is displaying a distinct list of state properties for the Store entity. In my understanding, the repository method provides query results of distinct state names ordered by the same. The controller converts this object to a list and the view outputs this list... but I'm getting this error and I have no idea why.
E-Madd
@E-Madd: See my updated answer.
Jesper Palm
@Jesper - Brilliant! Thank you so very much.
E-Madd