How to make SelectListItem Selected true or false on based on condition
<p>Here is my Model</p>

<pre><code>  public class NewsViewModel
    {
        public NewsViewModel()
        {

            this.Categories = new List&lt;Category&gt;();

        }
        public int NewsId { get; set; }
        [Required(ErrorMessage = "Please enter News Title")]
        public string NewsTitle { get; set; }
        public IEnumerable&lt;Category&gt; Categories { get; set; }


    }
</code></pre>

<p>I need to set Selected to be True if id exists in NewsViewModel.Categories collection</p>

<pre><code>private IEnumerable&lt;SelectListItem&gt; GetCategories()
{

    return db.Categories .Select(s=&gt;new SelectListItem {
        Value=s.Id.ToString(),
        Text=s.Name,
        Selected = model.Categories.Select(x =&gt; x.CategoryId).Contains(s.CategoryId);

}
</code></pre>

<p>And in View:</p>

<pre><code>@Html.ListBoxFor(s =&gt; s.SelectedCategoriesIds, @Model.AllCategories, new { id = "DropDownList2", multiple = "multiple", @class = "form-control" })
</code></pre>