0

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>
3
  • 1
    Can you explain in words what you are trying to do? Commented Jul 29, 2010 at 17:00
  • Per his message, he's getting an error in the aspx. Answered below. Commented Jul 29, 2010 at 17:03
  • What are you meaning to to with the select Convert.ToString(Store.State.Distinct()); line ? I don't thing it is what you actually want. Commented Jul 29, 2010 at 17:40

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! OK, now I'm getting a different error. "Sequence operators not supported for type 'System.String'"
1

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.

3 Comments

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'"
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.
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.
0

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.

1 Comment

Thanks. That was part of it for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.