[英]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. 我是.Net C#的新手,我真的不知道我做错了什么。 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.
我有一个LinqToSql DataContext,我正在从中绘制不同的结果,但是我不确定如何在我的视图中正确输出结果。 I'm getting a compilation error
(Compiler Error Message: CS1002: ; expected)
on my view. 我的视图上出现编译错误
(Compiler Error Message: CS1002: ; expected)
。
StoreRepository.cs StoreRepository.cs
public IQueryable<String> GetStoreStates()
{
return from Store in db.Stores
orderby Store.State
select Convert.ToString(Store.State.Distinct());
}
StoresController.cs StoresController.cs
public ActionResult StatesIndex()
{
var states = repo.GetStoreStates().ToList();
return View("StatesIndex", states);
}
StatesIndex.aspx StatesIndex.aspx
<ul>
<% foreach (var state in Model)
{ %>
<li>
<% Html.Encode(state) %>
</li>
<% } %>
</ul>
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. 如果您呼叫的一个
Html
扩展方法,你需要或者等号前缀它=
或冒号:
因为要显示这些方法输出相应的HTML字符串。 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. 如果要调用的方法不直接返回HTML字符串,则就像常规C#方法一样调用它,在这种情况下,将需要使用分号。
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. 记住何时开始使用MVC时,记住何时使用等号和何时使用分号可能会使您有点失望。
Your View model is a collection of strings, not store objects. 您的View模型是字符串的集合,而不是存储对象。 So when you are doing
所以当你做的时候
foreach(var Store in Model)
each Store
is only a string and you can't do Store.state
foreach(var Store in Model)
每个Store
只是一个字符串,您不能执行Store.state
Either change your GetStoreStates
method to return a list of store object or change the contents of your foreach to 更改您的
GetStoreStates
方法以返回存储对象列表或将foreach的内容更改为
<%= Html.Encode(Store) %>
Edit: Updated after comments. 编辑:评论后更新。
The problem is that you are trying to execute Distinct() on a string. 问题是您正在尝试对字符串执行Distinct()。 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. 这将在状态列表而不是每个状态字符串上执行Distinct()。
You need to repo.GetStoreStates().ToList()
. 您需要
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. 没有它,您将尝试将
IQueryable
作为模型传递,这实际上是尚未运行的查询。 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. 要运行查询,您需要调用
ToList()
,这将导致查询运行并返回List<type>
,然后可以循环遍历。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.