There is nothing wrong with using LINQ in a view as such. The problem is not that you are using LINQ, the problem is that you are doing this:
@{var lst = (from x in item.Showtimes select x.ShowtimeDate).Distinct();}
At any time when you need to declare and set a variable inside your view, perhaps this is an indication of the need to change your model. For this purpose, your view model must have a property for this. Something like that:
public IEnumerable<SomeType> DistinctShowtimes { get { return (from x in item.Showtimes select x.ShowtimeDate).Distinct(); } }
Then the LINQ point in the view becomes controversial, since all you need is:
@foreach (var showTimeItem in Model.DistinctShowtimes) { <option value="@showTimeItem">@showTimeItem</option> }
UPDATE (in response to your updated question):
Now the problem (albeit a bit smaller) is this:
@model IEnumerable<MelliConcert.Models.Concert>
While this works, it limits. And what you experience is a limitation. You ask yourself: "How can I return more than one look?" The answer is to create a custom view model for this view. Right now, your view is tied to listing Concert objects. This is normal if all this is needed. But it turns out that not everything is needed. It has some common logic that requires a bit more. Thus, you create a custom view model. Something like that:
public class ConcertsViewModel { public IEnumerable<Concert> Concerts { get; set; }
Then in the action of your controller, you return one of them:
public ActionResult Index() { using(var db = new Models.MelliConcertEntities()) { var concertsModel = new ConcertsModel(); concertsModel.Concerts = (from x in db.Concert orderby x.ID ascending select x).Take(15).ToList(); return View(concertsModel); } }
(Also note the use of the using statement, which should always be used when using IDisposable resources.)
So, now your view still receives a list of Concert objects, but it is packaged into a custom view model to which you can add additional features needed for this view. Then, in your opinion, change the model declaration:
@model MelliConcert.Models.ConcertsViewModel
(Itโs assumed that you put it in the Models namespace. Depending on the size of your application, you might want to bring the view models into your own namespace. I donโt often use the Models namespace in the actual application for the main business objects, so our projects are more likely everything is structured in very different ways. This should make you go, but you will want to make sure that your problems are carefully separated.)
Then, in the view code, you can refer to what you need from this object. Therefore, if you need to access the list, instead of just calling something like @foreach (var item in model) , you would @foreach (var item in model.Concerts) .