E-Madd
E-Madd

Reputation: 4582

Linq-To-Sql, MVC, Enumerable results with Distinct query

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>

Upvotes: 0

Views: 2083

Answers (3)

Jesper Palm
Jesper Palm

Reputation: 7238

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.

Upvotes: 1

Robaticus
Robaticus

Reputation: 23157

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.

Upvotes: 2

KallDrexx
KallDrexx

Reputation: 27803

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.

Upvotes: 0

Related Questions