AndrewC
AndrewC

Reputation: 6730

Using Any LINQ function to set model property

I have a model:

public class MyModel { 
    int id { get; set; }
    bool selected { get; set; }
}

In my view (which inherits IEnumerable<MyModel>) I call:

Html.EditorForModel()

This calls the subview (or whatever its called) and it displays a hidden field for the id and a checkbox for the selected bool.

I want to be able to pass in some checkboxes as already selected, so in my controller I am doing:

MyModel newmod = new MyModel { Id = 5, selected = (userselections.Any(t=> t.PlayerId == x.id) == true) }; 

Where userselections is a List of bojects with a PlayerId property.

However, I get the error:

Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.

Edit

This how userselections is populated:

var userselections = selectionRepository.Selection.Where(t => t.TeamId == curUser.TeamId).ToList();

Stack Trace

[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'

(Sorry, not sure how exactly format that. I've cut out all the stuff at the start, before System.Web.Mvc is called).

Upvotes: 0

Views: 417

Answers (2)

Bala R
Bala R

Reputation: 108937

How about using Contains ?

selected = (userselections.Select(t => t.PlayerID).ToList().Contains(x.id) == true)

Upvotes: 2

SLaks
SLaks

Reputation: 887285

Add .ToList() to your query to force it to be sent immediately to the client.

Upvotes: 1

Related Questions