我有一个模特:
public class MyModel {
int id { get; set; }
bool selected { get; set; }
}
在我看来(继承IEnumerable<MyModel>
)我打电话:
Html.EditorForModel()
这会调用子视图(或其调用的任何内容),它会显示id的隐藏字段和所选bool的复选框。
我希望能够传递一些已经选中的复选框,所以在我的控制器中我正在做:
MyModel newmod = new MyModel { Id = 5, selected = (userselections.Any(t=> t.PlayerId == x.id) == true) };
其中userselections是具有PlayerId属性的boject的列表。
然而,我收到错误:
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
修改
这是如何填充用户选择的:
var userselections = selectionRepository.Selection.Where(t => t.TeamId == curUser.TeamId).ToList();
堆栈跟踪
[NotSupportedException: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.]
System.Data.Linq.SqlClient.QueryConverter.CoerceToSequence(SqlNode node) +901537
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +5897
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +27
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +449
System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
System.Data.Linq.SqlClient.QueryConverter.VisitMemberInit(MemberInitExpression init) +449
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +222
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector) +160
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +1419
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +111
System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +114
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +132
System.Data.Linq.DataQuery`1.System.Collections.IEnumerable.GetEnumerator() +32
System.Web.Mvc.Html.DefaultEditorTemplates.CollectionTemplate(HtmlHelper html, TemplateHelperDelegate templateHelper) +896
System.Web.Mvc.Html.DefaultEditorTemplates.CollectionTemplate(HtmlHelper html) +68
System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames) +1594
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1616
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +86
System.Web.Mvc.Html.EditorExtensions.EditorForModel(HtmlHelper html) +91'
(对不起,不确定格式是多么准确。在调用System.Web.Mvc之前,我已经在开始时删除了所有内容。)
答案 0 :(得分:2)
如何使用Contains?
selected = (userselections.Select(t => t.PlayerID).ToList().Contains(x.id) == true)
答案 1 :(得分:1)
将.ToList()
添加到您的查询中,以强制将其立即发送给客户端。